Legenditem offers new possibilities.
However I'm wondering how I can ensure that the bar colors and the legend values are always synchronized.
I find pretty essential to avoid major mistake, especially when programming at a time the data are not frozen.
Here is an example.
The program works properly before sorting the data. But if the data sorting order change, the keylegend has to be updated because values in styleattrs are just indicating the colors for the first, second and third bar i.e. has no link with the other statements.
Is htere a way to keep using legenditem and avoiding such risk
data class;
set sashelp.class (where=(age=12))
sashelp.class (where=(age=14))
sashelp.class (where=(age=13));
agec=put(age,best.-l);
run;
*proc sort data=class;
*by agec;
*run;
%let ColorBarRed = CXBE030E;
%let ColorBarYellow = CXFFFF01;
%let ColorBarBlue = CX03B1F0;
proc sgplot data=class;
vbarbasic agec / response=height stat=mean group=agec;
legenditem type=fill name='Y12' / label='12 Years old'
fillattrs = graphdata1
outlineattrs= graphdata1;
legenditem type=fill name='Y14' / label='14 Years old'
fillattrs = graphdata2
outlineattrs= graphdata2;
legenditem type=fill name='Y13' / label='13 Years old'
fillattrs = graphdata3
outlineattrs= graphdata3;
styleattrs datacolors =(&ColorBarRed.
&ColorBarYellow.
&ColorBarBlue.);
keylegend 'Y12' 'Y14' 'Y13';
run;
When it's necessary to ensure that certain group levels are assigned the same fill color (or line type, or marker, etc.) then I might suggest using attribute maps. I use them frequently when I need to ensure that certain variables maintain a color scheme across different visualizations. This is not a detailed solution, but it should show the gist of how it works. The levels of "agec" will maintain their color scheme regardless of data sorting. (This blog post has a good summary if you are not familiar with them: https://blogs.sas.com/content/graphicallyspeaking/2013/04/02/attribute-maps-1/)
data attrmap;
length ID $ 10 Value $ 30 fillcolor $ 10;
infile datalines delimiter='|';
input ID Value fillcolor;
datalines;
agec|12|CXBE030E
agec|13|CX03B1F0
agec|14|CXFFFF01
;
run;
data class;
set sashelp.class (where=(age=12))
sashelp.class (where=(age=14))
sashelp.class (where=(age=13));
agec=put(age,best.-l);
run;
proc sort data=class;
by descending agec ;
run;
proc sgplot data=class dattrmap=attrmap;
vbarbasic agec / response=height stat=mean group=agec attrid=agec;
run;
When it's necessary to ensure that certain group levels are assigned the same fill color (or line type, or marker, etc.) then I might suggest using attribute maps. I use them frequently when I need to ensure that certain variables maintain a color scheme across different visualizations. This is not a detailed solution, but it should show the gist of how it works. The levels of "agec" will maintain their color scheme regardless of data sorting. (This blog post has a good summary if you are not familiar with them: https://blogs.sas.com/content/graphicallyspeaking/2013/04/02/attribute-maps-1/)
data attrmap;
length ID $ 10 Value $ 30 fillcolor $ 10;
infile datalines delimiter='|';
input ID Value fillcolor;
datalines;
agec|12|CXBE030E
agec|13|CX03B1F0
agec|14|CXFFFF01
;
run;
data class;
set sashelp.class (where=(age=12))
sashelp.class (where=(age=14))
sashelp.class (where=(age=13));
agec=put(age,best.-l);
run;
proc sort data=class;
by descending agec ;
run;
proc sgplot data=class dattrmap=attrmap;
vbarbasic agec / response=height stat=mean group=agec attrid=agec;
run;
You may find that two versions of discrete attribute maps are helpful, one with the SHOW='ATTRMAP' and one with SHOW='DATA'. When the SHOW option is set to ATTRMAP then all the values of the map are shown in the legend which may be helpful when making multiple graphs with some category that occasionally is missing but you want the Legend to be the same for each graph. The DATA option, which is the default when not specified, is to not show missing values in the legend.
May want to use explicit axis statements if the group variable is also an axis variable, such as creating different bar appearance for each bar of VBAR or HBAR type plots, to hold the space where a group with no values would appear.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.