How to make a cluster grouped bar chart graph using SAS SG procedures

5

Bar Charts are some of the most commonly used graphs to convey information to the reader.  Bar Charts are used across all domains, including business, finance, banking, clinical and health and life sciences.  Various kinds of Bar Chart can be created using the SAS Statistical Graphics (SG) procedures as shown below.  The data used for these graphs is shown at the bottom of this post.

Basic Bar Chart:

Basic Bar Chart
title 'Actual Sales by Product';
proc sgplot data=prdsale;
  vbar product / response=actual stat=sum nostatlabel;
  xaxis display=(nolabel);
  yaxis grid;
  run;


Stack Grouped Bar Chart:

An optional GROUP variable can be specified to create a stacked bar chart as shown below.

title 'Actual Sales by Product and Year';
proc sgplot data=prdsale;
  vbar product / response=actual stat=sum group=year nostatlabel;
  xaxis display=(nolabel);
  yaxis grid;
  run;


Cluster Grouped Bar Chart in SAS 9.2 (TS2M3):

In SAS 9.2 SG Procedures, the group variable always creates a stacked Bar Chart.  With SAS 9.3, a new option has been added to position the group values in side-by-side clusters instead of stacks as shown later in this article.  However, if you want to create a cluster grouped Bar Chart using SAS 9.2 SG procedures, you can create a close facsimile by using the SGPANEL procedure as shown below.  Except for the borders around the "category values" at the bottom (these are actually cell headers), this look pretty much the same as any cluster grouped Bar Chart.

Note the following options used on the PANELBY statement to get this result:

  • LAYOUT=columnlattice - Create a layout of coumns (one row)
  • ONEPANEL- Display all cells in one graph panel.
  • COLHEADERPOS=bottom - Move headers to the bottom of the cells
  • ROWS=1 - Arrange all cells in one row
  • NOVARNAME - Suppress the display of the class variable in the cell header.
  • NOBORDER- Suppress cell borders.
title 'Actual Sales by Product and Year';
proc sgpanel data=prdsale;
  panelby product / layout=columnlattice onepanel
          colheaderpos=bottom rows=1 novarname noborder;
  vbar year / group=year response=actual stat=sum group=year nostatlabel;
  colaxis display=none;
  rowaxis grid;
  run;


Cluster Grouped Bar Chart in SAS 9.3:

With SAS 9.3, a cluster grouped Bar Chart can be directly created using the SGPLOT procedure, by specifying the option GROUPDISPLAY=CLUSTER.  The default value for this option is STACK for backwards compatibility.  Using this option, you can create a cluster grouped Bar Chart as follows:

title 'Actual Sales by Product and Year';
proc sgplot data=prdsale;
  vbar product / response=actual stat=sum group=year nostatlabel
         groupdisplay=cluster;
  xaxis display=(nolabel);
  yaxis grid;
  run;


Bar charts with aesthetic skins in SAS 9.3

When used in the business domain, often there is a desire for a graph with a flashy appearance.   SG Procedures in SAS 9.3 support application of aesthetic skins to Bar Charts.  This option creates an aesthetically pleasing visual effect for the Bar Chart.  Various skin types are availabele for the DATASKIN option.

title 'Actual Sales by Product and Year';
proc sgplot data=prdsale;
vbar product / response=actual stat=sum group=year nostatlabel
       groupdisplay=cluster dataskin=gloss
xaxis display=(nolabel);
yaxis grid;
run;


Data

The data set prdsale used for the above examples is merely a cleaned up version of sashelp.prdsale as shown below.  You can easily use sashelp.prdsale for any of these examples.

data prdsale;
  set sashelp.prdsale;
  actual=actual/1000;
  predict=predict/1000;
  format actual predict dollar5.0;
  label actual='Actual (K)';
  label predict='Predict (K)';
  run;
Share

About Author

Sanjay Matange

Director, R&D

Sanjay Matange is R&D Director in the Data Visualization Division responsible for the development and support of the ODS Graphics system, including the Graph Template Language (GTL), Statistical Graphics (SG) procedures, ODS Graphics Designer and related software. Sanjay has co-authored a book on SG Procedures with SAS/PRESS.

5 Comments

  1. Ryan Hastings on

    Hello, I am using the code, Bar charts with aesthetic skins in SAS 9.3, can you change the bar colors. I am new to SAS and wanted to know if there was a simple code to assign the variables in the chart. I am making charts but my variable colors keep switching over years.

    • Sanjay Matange
      Sanjay Matange on

      The bar colors are based on active style. Each distinct class level in the group variable gets a color from the GraphData1-12 elements in the style. In SAS 9.3, the only way to customize group colors is by deriving a new style, and entering your own colors in the GraphData1-12 elements.

      In SAS 9.4, you can change the group colors to be used by using the new STYLEATTRS statement, where you can specify the list of DataColors, DataContrastColors, DataSymbols and DataLinePatterns. Please refer to the SGPLOT document.

  2. How do I change the order of the variables in the stacked bar chart using sas 9.2 and proc sgplot? Let's say you wanted 1994 as the bottom part of the graph with the 1993 on top, the opposite of what your chart is doing?

Back to Top