PROC SGPLOT - Categorical data

Last changed: 22 December 2020

Import the file jobdata.xlsx to SAS or use the SAS-file The data set consists of a variable job (A,B or C) a varaible city (1, 2 or 3) and a variable answer (Yes, No).

Bar charts

We are interested how the answer varies with jobs.

proc sgplot data=jobdata;
vbar job / group=answer;

or the other way round, depending on what we want to highlight:

proc sgplot data=jobdata;
vbar answer / group=job;


To set the different bars side-by-side (in SAS 9.3 or above):

proc sgplot data=categ;
bar job / group=answer groupdisplay=cluster;


Display percentages in plots 

Start by computing the percentages: 

proc freq data=categ;
tables answer*job /  out=Freq1Out;

Then use the percentages in the plot: 

proc sgplot data=Freq1Out;
vbar job / group=answer groupdisplay=cluster respone=percent;

Now, the labeling of the plot does not look very good. To change to percentage values we need to use a formatting:

data freq2Out;
set freq1out;
format pct percent7.1;

percent7.1 is a percent format with one decimal place. The value we format needs to be between 0 and 1. Now try the plot again:

proc sgplot data=freq2Out;
vbar job / group=answer groupdisplay=cluster response=pct ;
yaxis label="Percentage";