Hi,
I’m having trouble in the following bar plot.
My goals are:
- Descending order by the total count of each key word
- The same order of two teams inside each bar
The code I’m using now is as below:
proc sort data=rct;
by descending totalct team;
run;
proc sgplot data = rct; VBAR keyword /RESPONSE = count /*y-axis: count per keyword per team*/ group = team /*stack each bar with two teams*/ categoryorder = respdesc; /*order by count of each team per keyword, not the total count of each keyword*/ run;
Part of the data is like below:
I know the above code is not 100% correct in terms of achieving my goals, but I don’t know what code (proc or options) to use in this case.
Please advise.
Thanks!
Alicia
Hi Alicia,
Okay, I see. Try this code:
data rct2;
set rct;
totalct_y = totalct + 10;
run;
proc sgplot data = rct2;
vbarparm category=keyword response = count / group = team grouporder = data;
scatter x = keyword y = totalct_y / markerchar= totalct;
run;
Hi Alicia,
Your code looks correct. Have you tried to sort your data by descending count totalct team?
Many thanks,
Kriss
Hi Kriss,
Thanks for your advice. I tried your sorting but unfortunately the issues are still there (the output stays the same).
Thanks!
Alicia
Hi Alicia,
Okay, I see. Try this code:
data rct2;
set rct;
totalct_y = totalct + 10;
run;
proc sgplot data = rct2;
vbarparm category=keyword response = count / group = team grouporder = data;
scatter x = keyword y = totalct_y / markerchar= totalct;
run;
This works, Kriss. Wonderful!
I don't quite understand why using "totalct_y = totalct + 10" in your data step. Could you explain a bit?
Thanks so much!
read this article.
Solved: sgplot hbar - Is it possible to sort categories by... - SAS Support Communities
I see that there's an example of a stacked bar plot there that has blue and red in the correct order. Good luck.
Hi PhilC,
Thanks for pulling up the previous case - it was exactly what I wanted to do. The only thing is when I was trying to do the complicated sql step (I'm entry level), SAS kept giving errors although I replaced my variables accordingly. I saved the solution for future use. It was absolutely helpful!
Regards,
Alicia
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.