## Group data on a plot by multiple grouping variables

Solved
Occasional Contributor
Posts: 14

# Group data on a plot by multiple grouping variables

I wonder if it's possible in GTL to group values by several variables using different attributes for them. For example, I want to plot a scatter plot for sashelp.class where x=Weight and y=Height and where points will be color-coded by Age (11=red, 12=blue etc) and symbol-coded for different Sexes (male=X, female=Circle).

Accepted Solutions
Solution
‎02-14-2014 09:58 PM
Posts: 5,621

## Re: Group data on a plot by multiple grouping variables

I don't think you can group by more than one variable. If you don't need a graph legend, you can do:

data sClass / view=sClass;

set sashelp.class;

if sex="M"

then mHeight = height;

else fHeight = height;

label mHeight="Height" fHeight="Height";

run;

proc sgPlot data=sClass noautolegend;

scatter x=weight y=mHeight / group=age markerAttrs=(symbol="X");

scatter x=weight y=fHeight / group=age markerAttrs=(symbol="Circle");

run;

PG

PG

All Replies
Solution
‎02-14-2014 09:58 PM
Posts: 5,621

## Re: Group data on a plot by multiple grouping variables

I don't think you can group by more than one variable. If you don't need a graph legend, you can do:

data sClass / view=sClass;

set sashelp.class;

if sex="M"

then mHeight = height;

else fHeight = height;

label mHeight="Height" fHeight="Height";

run;

proc sgPlot data=sClass noautolegend;

scatter x=weight y=mHeight / group=age markerAttrs=(symbol="X");

scatter x=weight y=fHeight / group=age markerAttrs=(symbol="Circle");

run;

PG

PG
Occasional Contributor
Posts: 14

## Re: Group data on a plot by multiple grouping variables

Thanks, PGStats. Looks like it's the only way to do what I want so far. So I'll need to create legend in some other way (as an additional text element, e.g.). I also found MERGEDLEGEND statement that seems to be a step towards creating multigrouped plots, but it only works for two plots and of certain types (line and marker). Would be great if SAS had the ability to group data by multiple variables or at least to customize legend (so that I could display legend for one of the plots and then change legend's markers from X's or circles to, e.g., squares, emphasizing by this that color matters, not shape).

Posts: 5,621

## Re: Group data on a plot by multiple grouping variables

Good luck. You may also get some level of control by combining your group variables (i.e. sexAge = cats(sex,age); ) and using SG Attribute Maps. SAS(R) 9.3 ODS Graphics: Procedures Guide, Third Edition - PG

PG
SAS Super FREQ
Posts: 1,289

Posts: 5,621

## Re: Group data on a plot by multiple grouping variables

A simpler, decent alternative is :

proc sgPlot data=sashelp.Class;

scatter x=weight y=height / group=age grouporder=ascending markerchar=sex;

run;

PG

PG
SAS Super FREQ
Posts: 1,289

## Re: Group data on a plot by multiple grouping variables

And if one of your variables is discrete and you want clustered groups (side by side) you can use GROUPDISPLAY=cluster.

SAS Super FREQ
Posts: 4,270

## Re: Group data on a plot by multiple grouping variables

As PG says, combine your groups into a single variable (you can use PROC FREQ for this). Then use data attributes, as shown in this blog post Specify the colors of groups in SAS statistical graphics - The DO Loop

🔒 This topic is solved and locked.