Hello all,
I was wondering if there is any capacity within PROC SGPLOT for traffic lighting (i.e. for instance certain points in a SCATTER plot will be colored red if they fall within a certain range otherwise they will be green, etc.)? I searched this online and could not find out anything solid. I know that PROC REPORT has traffic lighting options but I prefer to stick to PROC SGPLOT as it may take some time for me to figure out how to produce figures with PROC REPORT if that's even possible.
If you can direct me to a source if traffic lighting is possible within PROC SGPLOT, I would really appreciate it.
Thanks in advance!
Recep
Look at RATTRMAP
Example from the documentation that uses a data set SASHELP.CLASS to plot that you should have.
data myrattrmap; retain id "myID"; length min $ 5 max $ 5; input min $ max $ color $ altcolor $; datalines; _min_ 80 purple purple 80 100 gold gold 100 _max_ green green ; run; proc sgplot data=sashelp.class rattrmap=myrattrmap; scatter x=height y=weight / colorresponse=weight rattrid=myID; run;
The variable names have to match the expected properties of an RATTRMAP data set.
Thanks @ballardw ! Your solution seems like creating static precalculated ranges in the previous data step. I tried to calculate the color responses I want in a previous data step (please see the example below) yet I'm not getting exactly what I need (though I fully acknowledge that I did not replicate your data step in your proposed solution). Note the if/then statement in the second data step.
Basically I was hoping that, based on the if/then statement, QC and AB in panel "1&2" would be colored green (because cupper<obsmean) and QC in panel "3" would be colored red (because clower>obsmean).
data test1;
input level $ obsmean site $ cpred clower cupper;
datalines;
1&2 11.2716763 AB 11.2091 10.2363 12.2633
3 9.648187633 AB 6.7446 5.4658 8.2922
1&2 11.2716763 BC 10.953 10.0474 11.9304
3 9.648187633 BC 8.4634 7.2455 9.86
1&2 11.2716763 NB 12.8006 10.4527 15.6237
1&2 11.2716763 ON 12.2879 10.6616 14.1237
1&2 11.2716763 QC 9.1078 8.3792 9.8939
3 9.648187633 QC 13.7365 12.3518 15.2589
;
run;
data test2;
length color $5.;
set test1;
if clower>obsmean then color='red';
if cupper<obsmean then color='green';
run;
proc sgpanel data=test2;
panelby level/columns=2 novarname sort=data onepanel;
rowaxis values=(0 to 15 by 5) offsetmin=.05 offsetmax=.05 grid label=" ";
colaxis offsetmin=.2 offsetmax=.2 grid label=" ";
scatter x=site y=cpred/yerrorupper=cupper
yerrorlower=clower
MARKERATTRS=(symbol=squarefilled color=black size=10)
TRANSPARENCY=0.3
DATALABEL=cpred
DATALABELPOS=right
DATALABELATTRS=(color=black weight=bold size=5)
colorresponse=cpred;
refline obsmean;
format cpred 4.1;
title ' ';
run;
If you want to use an "if/then" assignment then you make that variable a group variable in a scatter plot.
Then you use either a styleattrs statement to override the color assignments or create a Dattrmap data set to set colors, markers, size, line types based on the values of the group variable.
To see what a default style difference would make try this:
proc sgpanel data=test2; panelby level/columns=2 novarname sort=data onepanel; rowaxis values=(0 to 15 by 5) offsetmin=.05 offsetmax=.05 grid label=" "; colaxis offsetmin=.2 offsetmax=.2 grid label=" "; scatter x=site y=cpred/yerrorupper=cupper yerrorlower=clower group=color /*<= this tells SAS to use levels of the variable color to change attributes*/ MARKERATTRS=( size=10) TRANSPARENCY=0.3 DATALABEL=cpred DATALABELPOS=right DATALABELATTRS=(color=black weight=bold size=5) colorresponse=cpred; refline obsmean; format cpred 4.1; title ' '; run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.