Plot Groups Using PROC SGPLOT in SAS

Proc SGPLOT in SAS is a procedure used to plot groups of data. It provides graphical displays of data, such as line plots, scatter plots, and histograms, to aid in the analysis of data. It also allows for the customization of the plot’s appearance by allowing users to control the color, shape, and size of data points. Additionally, it allows for the addition of labels, titles, and legends to the plot for easier interpretation.


You can use the following methods to plot groups using PROC SGPLOT in SAS:

Method 1: Plot Each Group in Separate Charts

/*create multiple plots that show histogram of points for each team*/
proc sgplot data=my_data; 
   by team;
   histogram points;
   density points / type=kernel;
run;

The by statement tells SAS to create a separate histogram for each unique value in the team column.

Method 2: Plot Each Group in One Chart

/*plot histogram of points for each team on one chart*/
proc sgplot data=my_data; 
   histogram points / group=team;
   density points / type=kernel group=team;
run;

The group statement tells SAS to display a separate histogram for each unique value in the team column overlaid on the same chart.

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

/*create dataset*/
data my_data;
    input team $ points;
    datalines;
A 29
A 23
A 20
A 21
A 33
A 35
A 31
B 21
B 14
B 15
B 11
B 12
B 10
B 15
;
run;

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

Example 1: Use PROC SGPLOT to Plot Each Group in Separate Charts

We can use the by statement in PROC SGPLOT to create individual histograms that show the distribution of points for each team:

/*create multiple plots that show histogram of points for each team*/
proc sgplot data=my_data; 
   by team;
   histogram points;
   density points / type=kernel;
run;

The first histogram displays the distribution of points for team A and the second histogram displays the distribution of points for team B.

Note: The density statement is optional, but it displays a for each histogram which can be a helpful way to summarize the shape of the distribution.

Example 2: Use PROC SGPLOT to Plot Each Group in One Chart

We can use the group statement in PROC SGPLOT to create one chart that contains overlaid histograms of the distribution of points for each team:

/*plot histogram of points for each team on one chart*/
proc sgplot data=my_data; 
   histogram points / group=team transparency=0.5;
   density points / type=kernel group=team;
run;

SAS PROC SGPLOT by group

The blue histogram displays the distribution of points values for team A and the red histogram displays the distribution of points values for team B.

The benefit of using this approach is that we can quickly compare the distribution of points values for each team since each histogram is placed on the same chart.

Note: The transparency argument makes the histograms transparent and easier to read when they overlap. The closer you set this value to 1, the more transparent the histograms become.

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

x