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

Hi, 

How can I change the order of the variables that show up in the legend of this bar chart? I would like it to show: level 1-2-3-4 (and not level 3-1-2-4) (cf picture). The variables do show up in the right order on the graph (with level1 starting at y=0, then level2 stacked on top of level1 and so on). Is there a way I can input the exact legend I want SAS to show, without changing my data? If not, here's what my code and data look like: 

 

 

Capture d’écran, le 2021-09-15 à 11.46.53.png

 

proc sort data=A;
by patient_ID;

proc transpose data=A
out=long(where=(col1 ne 0));
by patient_ID episode_start_time;
var level:;
run;

data level_data;
set long;
level = substr(compress(reverse(_name_)), 1, 1);
run;

/*monthly bar chart */
proc sort data=level_data;
by episode_start_time level;

proc sgplot data=level_data;
vbar episode_start_time/ group=level datalabel seglabel;
format episode_start_time MONYY7.;
run;

level_data (sorted by episode_start_time and level) looks like this: 

patient_IDepisode_start_time_name__label_col1level
116/01/2021level3level313
206/02/2020level1level111
309/02/2020level1level111
411/02/2020level1level111
503/03/2020level4level414
605/03/2020level1level111
...     
...     

 

Hope that's enough information. Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
blueskyxyz
Lapis Lazuli | Level 10
data legend_order;
  retain Id 'Origin' Show 'Attrmap';
  length Value $10 FillStyleElement $15;
  input value $ FillStyleElement $;
  datalines;
1     graphdata1
2     graphdata2
3     graphdata3
4     graphdata4
;
run;


proc sgplot data=level_data dattrmap=legend_order;
    vbar episode_start_time/ group=level datalabel seglabel 
                            attrid=Origin /*added*/;
    format episode_start_time MONYY7.;
run;

Legend Order - Graphically Speaking (sas.com)

View solution in original post

5 REPLIES 5
blueskyxyz
Lapis Lazuli | Level 10
data legend_order;
  retain Id 'Origin' Show 'Attrmap';
  length Value $10 FillStyleElement $15;
  input value $ FillStyleElement $;
  datalines;
1     graphdata1
2     graphdata2
3     graphdata3
4     graphdata4
;
run;


proc sgplot data=level_data dattrmap=legend_order;
    vbar episode_start_time/ group=level datalabel seglabel 
                            attrid=Origin /*added*/;
    format episode_start_time MONYY7.;
run;

Legend Order - Graphically Speaking (sas.com)

sasthalie
Fluorite | Level 6

This works out great, thank you so much!

Ksharp
Super User
Sort you data by LEVEL firstly .

proc sort data=level_data;
by level; /*<==========*/

proc sgplot data=level_data;
vbar episode_start_time/ group=level datalabel seglabel;
format episode_start_time MONYY7.;
run;
sasthalie
Fluorite | Level 6

The only issue that comes up with this is that the x categories are out of order (see picture): Jan2020 shows up last. 

 

 Capture d’écran, le 2021-09-16 à 13.33.32.png

Ksharp
Super User
Could try VALUES= option.

proc sgplot data=level_data;
vbar episode_start_time/ group=level datalabel seglabel;
format episode_start_time MONYY7.;
xaxis values=('01jan2020'd to '01mar2021'd by month) ;
run;