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).
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
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
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).
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
A simpler, decent alternative is :
proc sgPlot data=sashelp.Class;
scatter x=weight y=height / group=age grouporder=ascending markerchar=sex;
run;
PG
And if one of your variables is discrete and you want clustered groups (side by side) you can use GROUPDISPLAY=cluster.
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
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!
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.
Ready to level-up your skills? Choose your own adventure.