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:
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_ID | episode_start_time | _name_ | _label_ | col1 | level |
| 1 | 16/01/2021 | level3 | level3 | 1 | 3 |
| 2 | 06/02/2020 | level1 | level1 | 1 | 1 |
| 3 | 09/02/2020 | level1 | level1 | 1 | 1 |
| 4 | 11/02/2020 | level1 | level1 | 1 | 1 |
| 5 | 03/03/2020 | level4 | level4 | 1 | 4 |
| 6 | 05/03/2020 | level1 | level1 | 1 | 1 |
| ... | |||||
| ... |
Hope that's enough information. Thank you!
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;
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;
This works out great, thank you so much!
The only issue that comes up with this is that the x categories are out of order (see picture): Jan2020 shows up last.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.