Data visualization with SAS programming

Order xaxis by descending frequency in sgplot

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 102
Accepted Solution

Order xaxis by descending frequency in sgplot

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?


Accepted Solutions
Solution
‎07-03-2014 11:12 AM
SAS Employee
Posts: 963

Re: Order xaxis by descending frequency in sgplot

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


All Replies
SAS Super FREQ
Posts: 1,042

Re: Order xaxis by descending frequency in sgplot

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

Frequent Contributor
Posts: 102

Re: Order xaxis by descending frequency in sgplot

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.

SAS Super FREQ
Posts: 1,042

Re: Order xaxis by descending frequency in sgplot

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.

Attachment
Solution
‎07-03-2014 11:12 AM
SAS Employee
Posts: 963

Re: Order xaxis by descending frequency in sgplot

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

Frequent Contributor
Posts: 102

Re: Order xaxis by descending frequency in sgplot

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

Post a Question
Discussion Stats
  • 5 replies
  • 2144 views
  • 3 likes
  • 3 in conversation