# SAS: How to Plot Groups Using PROC SGPLOT

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 density curve 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;```

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.