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

I have some data on adverse events that I'd like to show in sgplot. I want the xaxis to be ordered by descending frequency.

When I use the CATEGORYORDER=RESPDESC it works. However, when I want to group the vbar this option is ignored. 

proc sgplot data=AE_data;

  vbar site_ID / CATEGORYORDER=RESPDESC group=severity ; 

run;


I do not want to use discreteorder=data because this will order the data by Site_ID, not by the frequency of site_id. How do I get an output with the vbar sorted by frequency of site_id?

1 ACCEPTED SOLUTION

Accepted Solutions
GraphGuy
Meteorite | Level 14

If sgplot can't easily create the chart you want, perhaps Proc Gchart would be a better choice?...

proc gchart data=sashelp.cars;

vbar type / type=freq outside=freq subgroup=origin descending;

run;

foo.png

View solution in original post

5 REPLIES 5
Jay54
Meteorite | Level 14

Yes, I see the problem.  When GROUP is specified, you are getting group order of descending response, but discrete order of ascending (default).  This is a bug we will need to addess.

The way out I see is to summarize the data using proc means, sort by descending (sum of) frequencies and then use VBAR to draw the data with N as the response variable and DiscreteOrder=Data.  Here is an example.

FreqPlot.png

sarahsasuser
Quartz | Level 8

Thanks Sanjay. Because the data are character, I ran a PROC FREQ on the site_id, output those counts to a dataset, and merged this new freq dataset with the original dataset. Then I sorted on frequency before plotting. Is there an easier/smarter way to obtain frequencies without merging the output and original datasets? I have multiple variables that I need to graph and then plot by descending frequency in this dataset. Therefore, I'm running several frequencies and outputing to multiple datasets and then merging these datasets with the original.

Jay54
Meteorite | Level 14

I did it a little differently.  My code is attached.  Only issue in this case is that the segments within the bars are not correctly sorted in descending order due to another bug in SGPLOT.  :smileyplain:  But, if you don't care about that, you are good to go.  If you do care about it, then you need another adjustment.

GraphGuy
Meteorite | Level 14

If sgplot can't easily create the chart you want, perhaps Proc Gchart would be a better choice?...

proc gchart data=sashelp.cars;

vbar type / type=freq outside=freq subgroup=origin descending;

run;

foo.png

sarahsasuser
Quartz | Level 8

Yes, that's much easier. Using SGPLOT, I have to create frequency datasets, merge those datasets with the original, and then plot.

SAS Innovate 2025: Register Now

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!

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
  • 5 replies
  • 6073 views
  • 3 likes
  • 3 in conversation