Using PROC SGPLOT to Produce Box Plots with Contrasting Colours in SAS

I previously explained the statistics behind box plots and how to produce them in R in a very detailed tutorial.  I also illustrated how to produce side-by-side box plots with contrasting patterns in R.

Here is an example of how to make box plots in SAS using the VBOX statement in PROC SGPLOT.  I modified the built-in data set SASHELP.CLASS to generate one that suits my needs.

The PROC TEMPLATE statement specifies the contrasting colours to be used for different classes.  I also include code for exportingthe result into a PDF file using ODS PDF.  (I used varying shades of grey to allow the contrast to be shown when printed in black and white.)


There are some very useful SAS documents on the different colours and marker symbols.  There is a separate documentation page colour schemes in SAS/GRAPH programs, and it has a specific section on grey-scale colour codes.

ods path(prepend) 

proc template;
          define style mystyle;
           parent = styles.printer;
          class graphdata1 / 
                     color = grayaa 
                     contrastcolor = gray00 
                     markersymbol = 'circle';
          class graphdata2 / 
                     color = graycc 
                     contrastcolor = grayaa 
                     markersymbol = 'square';

data heights;
           set sashelp.class;
           if age < 13
                     then teenager = 'Pre-Teenager';
         else teenager = 'Teenager';
          if sex = 'F'
                     then gender = 'Female';
           else gender = 'Male';
           label            gender = 'Gender'
                               teenager = 'Age Group';
          keep height gender teenager;

ods pdf file = 'INSERT YOUR DIRECTORY PATH HERE\heights.pdf' 
           notoc style = mystyle;

proc sgplot 
           data = heights;
           vbox height / 
                     category = gender 
                     group = teenager;

ods pdf close;

Your thoughtful comments are much appreciated!

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: