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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 6 replies
  • 1317 views
  • 2 likes
  • 3 in conversation