BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Dmitry
Obsidian | Level 7

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).

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

View solution in original post

7 REPLIES 7
PGStats
Opal | Level 21

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
Dmitry
Obsidian | Level 7

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).

PGStats
Opal | Level 21

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
PGStats
Opal | Level 21

A simpler, decent alternative is :

proc sgPlot data=sashelp.Class;

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

run;

PG

PG
Jay54
Meteorite | Level 14

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

Rick_SAS
SAS Super FREQ

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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 26959 views
  • 7 likes
  • 4 in conversation