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

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 

stack bars.png

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:

data.png

 

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

1 ACCEPTED SOLUTION

Accepted Solutions
djrisks
Barite | Level 11

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;

View solution in original post

6 REPLIES 6
djrisks
Barite | Level 11

Hi Alicia,

 

Your code looks correct. Have you tried to sort your data by descending count totalct team?

 

Many thanks,

 

Kriss

garfieldyue
Fluorite | Level 6

Hi Kriss,

 

Thanks for your advice. I tried your sorting but unfortunately the issues are still there (the output stays the same).

 

Thanks!

Alicia

djrisks
Barite | Level 11

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;

garfieldyue
Fluorite | Level 6

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!

PhilC
Rhodochrosite | Level 12

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.

garfieldyue
Fluorite | Level 6

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2090 views
  • 2 likes
  • 3 in conversation