BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
xxformat_com
Barite | Level 11

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;    

 

1 ACCEPTED SOLUTION

Accepted Solutions
svh
Lapis Lazuli | Level 10 svh
Lapis Lazuli | Level 10

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; 

View solution in original post

3 REPLIES 3
svh
Lapis Lazuli | Level 10 svh
Lapis Lazuli | Level 10

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; 
xxformat_com
Barite | Level 11
Replacding styleattrs with the mapping table is a great idea. Thank you.
ballardw
Super User

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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 352 views
  • 1 like
  • 3 in conversation