How to Create a Bar Chart in SAS (with Examples)

Deepanshu Bhalla Add Comment

In this tutorial, we will cover how to create a bar chart in SAS, along with examples.

Syntax for Creating a Bar Chart in SAS

The code below shows the syntax to create different types of Bar Charts in SAS.

Bar Chart
proc sgplot data = dataset-name;
vbar variable1;
run;
Horizontal Bar Chart
proc sgplot data = dataset-name;
hbar variable1;
run;
Stacked Bar Chart
proc sgplot data = dataset-name;
vbar variable1 / group = variable2 groupdisplay=stack;
run;
Clustered Bar Chart
proc sgplot data = dataset-name;
vbar variable1 / group = variable2 groupdisplay = cluster;
run;
Stacked Horizontal Bar Chart
proc sgplot data = dataset-name;
hbar variable1 / group = variable2 groupdisplay=stack;
run;
Clustered Horizontal Bar Chart
proc sgplot data = dataset-name;
hbar variable1 / group = variable2 groupdisplay = cluster;
run;
Segmented Bar Chart
proc sgplot data=dataset-name pctlevel=group;
vbar variable1 / group=variable2 groupdisplay=stack stat=pct;
run;
Dataset

In this tutorial, we will be using the sashelp.cars dataset which is a built-in dataset in SAS that contains information about various car models.

Table of Contents

Bar Chart

A bar chart uses vertical bars to represent data. Each bar's height corresponds to the data's value, and the bars are displayed from the bottom to the top. It's useful for comparing values across categories or when category labels are short. The vertical bar chart is also called a Column Chart.

Simple Bar Chart

The following code uses the SGPlot procedure to create a vertical bar chart (vbar) of the "type" variable from the "sashelp.cars" dataset. The chart will display the frequency or count of each distinct value of the "type" variable.

proc sgplot data=sashelp.cars;
vbar type;
run;
SAS: Bar Chart
  1. proc sgplot: This is the procedure to create statistical graphics (SG) plots in SAS.
  2. data=sashelp.cars: This specifies the dataset "sashelp.cars" that contains the data to be used for creating the bar chart.
  3. vbar type;: This is the vbar statement that creates the vertical bar chart. "type" is the variable that will be used for the bar chart.

Stacked Bar Chart

The following code uses the SGPlot procedure to create a stacked bar chart showing the distribution of car types based on their countries of origin using data from the sashelp.cars dataset.

proc sgplot data = sashelp.cars;
vbar type / group = origin groupdisplay=stack;
run;
SAS: Stacked Bar Chart
  • group = origin: It tells SAS to use the origin variable as the grouping variable. The origin variable in the sashelp.cars dataset represents the country of origin for each car model.
  • groupdisplay=stack: This option specifies that the bars representing different groups (in this case, the countries of origin) will be stacked on top of each other, forming a stacked bar chart.

Clustered Bar Chart

The following code uses the SGPlot procedure to create a clustered bar chart showing the distribution of car types based on their countries of origin using data from the sashelp.cars dataset.

proc sgplot data=sashelp.cars;
vbar type / group=origin groupdisplay=cluster;
run;
SAS: Clustered Bar Chart

groupdisplay=cluster: This option specifies that the bars representing different groups (in this case, the countries of origin) will be clustered side by side in the bar chart.

Segmented Bar Chart

A segmented bar chart is a type of bar chart that shows multiple sets of data on a single bar, segmented into different categories. It is also called a stacked bar chart with multiple segments. In a segmented bar chart, each bar represents a total, and the segments of the bar represent different categories that make up that total.

The following code uses the PROC SGPLOT procedure to create a segmented bar chart using the sashelp.cars dataset.

proc sgplot data=sashelp.cars pctlevel=group;
vbar type / group=origin groupdisplay=stack stat=pct;
run;
Segmented Bar Chart

Horizontal Bar Chart

A horizontal bar chart uses horizontal bars to show data. Each bar's length corresponds to the data's value, and the bars go from left to right. It's useful for comparing values across categories or when category labels are long.

Simple Horizontal Bar Chart

The following code uses the SGPlot procedure to create a horizontal bar chart (hbar) of the "type" variable from the "sashelp.cars" dataset.

proc sgplot data = sashelp.cars;
hbar type;
run;
SAS: Horizontal Bar Chart

Stacked Horizontal Bar Chart

The following code uses the SGPlot procedure to create a stacked horizontal bar chart showing the distribution of car types based on their countries of origin using data from the sashelp.cars dataset.

proc sgplot data = sashelp.cars;
hbar type / group = origin groupdisplay=stack;
run;
Stacked Horizontal Bar Chart

Clustered Horizontal Bar Chart

The following code uses the SGPlot procedure to create a clustered horizontal bar chart showing the distribution of car types based on their countries of origin using data from the sashelp.cars dataset.

proc sgplot data=sashelp.cars;
hbar type / group=origin groupdisplay=cluster;
run;
Clustered Horizontal Bar Chart

How to Add Data Labels in a Bar Chart in SAS

The datalabel option tells SAS that data labels will be displayed on top of each bar in the bar chart. The data labels show the actual values corresponding to each bar, making it easier to read and understand the values represented by the bars.

proc sgplot data=sashelp.cars;
vbar type / datalabel;
run;
Add Data Labels in a Bar Chart in SAS

How to Sort Bars in a Bar Chart in SAS

The CATEGORYORDER= option tells SAS to sort the bars based on the values. You can use CATEGORYORDER = RESPASC for ascending order and CATEGORYORDER = RESPDESC for descending order.

proc sgplot data=sashelp.cars;
vbar type / categoryorder=respdesc;
run;
Sort Bars in a Bar Chart in SAS

How to Add Percentage in a Bar Chart in SAS

The following code uses the PROC SGPLOT procedure to create a bar chart with percentages using the sashelp.cars dataset.

proc sgplot data=sashelp.cars;
vbar type / stat=pct;
run;
SAS: Percent Bar Chart

stat=pct: This option specifies that the percentages will be displayed on the bar chart. It will show the percentage distribution of each car type relative to the total count of car types.

How to Customize Bar Chart in SAS

title "Bar Chart - Cars Dataset";
proc sgplot data=sashelp.cars;
    vbar type / fillattrs=(color=red);
    xaxis label="Car Type";
    yaxis label="Count";
run;
  • title "Bar Chart - Cars Dataset";: Sets the title of the chart to "Bar Chart - Cars Dataset".
  • fillattrs=(color=red);: It sets the color of the bars to red.
  • xaxis label="Car Type";: Sets the label for the X-axis of the chart to "Car Type".
  • yaxis label="Count";: Sets the label for the Y-axis of the chart to "Count".
Style and color Bar Chart in SAS
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "How to Create a Bar Chart in SAS (with Examples)"
Next → ← Prev