How to Create Bar Charts in SAS (3 Examples)


You can use the following methods to create different types of bar charts in SAS:

Method 1: Create One Bar Chart

proc sgplot data = my_data;
    vbar variable1;
run;

Method 2: Create Stacked Bar Chart

proc sgplot data = my_data;
    vbar variable1 / group = variable2;
run;

Method 3: Create Clustered Bar Chart

proc sgplot data = my_data;
    vbar variable1 / group = variable2 groupdisplay = cluster;
run;

The following examples show how to use each method with the following dataset in SAS:

/*create dataset*/
data my_data;
    input team $ position $ points;
    datalines;
A Guard 8
A Guard 6
A Guard 6
A Forward 9
A Forward 14
A Forward 11
B Guard 10
B Guard 9
B Guard 5
B Forward 7
C Guard 10
C Forward 6
C Forward 8
;
run;

/*view dataset*/
proc print data=my_data;

Example 1: Create One Bar Chart

The following code shows how to create a bar chart to visualize the frequency of teams:

/*create bar chart to visualize frequency of teams*/
title "Bar Chart of Team Frequency";
proc sgplot data = my_data;
    vbar team;
run;

bar chart in SAS

If you’d like a horizontal bar chart instead, simply use the hbar option:

/*create horizontal bar chart to visualize frequency of teams*/
title "Bar Chart of Team Frequency";
proc sgplot data = my_data;
    hbar team;
run;

horizontal bar chart in SAS

Example 2: Create Stacked Bar Chart

The following code shows how to create a stacked bar chart to visualize the frequency of both team and position:

/*create stacked bar chart*/
title "Stacked Bar Chart of Team & Position";
proc sgplot data = my_data;
    vbar team / group = position;
run;

stacked bar chart in SAS

This chart allows us to visualize the frequency of each team along with the frequency of positions within each team.

Example 3: Create Clustered Bar Chart

The following code shows how to create a clustered bar chart to visualize the frequency of both team and position:

/*create clustered bar chart*/
title "Clustered Bar Chart of Team & Position";
proc sgplot data = my_data;
    vbar team / group = position groupdisplay = cluster;
run;

clustered bar chart in SAS

This bar chart displays the same information as the previous bar chart, except the bars are “clustered” together instead of stacked on top of each other.

Additional Resources

The following tutorials explain how to create other charts in SAS:

How to Create Line Plots in SAS
How to Create Histograms in SAS
How to Create Boxplots by Group in SAS
How to Create a Scatterplot with Regression Line in SAS

Leave a Reply

Your email address will not be published.