Data visualization with SAS programming

Grouped Bar Chart, Mean line

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Grouped Bar Chart, Mean line

Hello!

 

I am trying to produce a graph like below:

 

hisotgram.JPG

 

The line should connect the mean amongst the groups at each Study Week.

 

I understand how to do multiple lines connecting each group across Study Weeks, but how can I produce one line to connect the means for each Study Week?


This is my code (which does not work):

 

proc sgplot data=alldat0&testcd ;
  
by subject;
vbar week/
 group=vstptnumm
 groupdisplay = CLUSTER 
 response=vsstresn 
 ;

 vline week/response=vsmean y2axis;
 
 
 format vstptnumm $vstim.;

run;

Vline needs to have the group= option as well. 

 

Any advice? Thanks so much!

Monica

 

 

 


Accepted Solutions
Solution
‎11-07-2016 12:09 PM
SAS Super FREQ
Posts: 864

Re: Grouped Bar Chart, Mean line

Currently, there are restrictions that prevent VLINE from working for you in this scenario. However, there is a solution. You can precompute the values in PROC MEAN and use VBARPARM and SERIES to display the result. Here is an example:

 

proc means data=sashelp.cars nway;
class origin type;
var mpg_city;
output out=bardata mean=;
run;

proc means data=sashelp.cars nway;
class origin;
var mpg_city;
output out=linedata mean=;
run;

data merged;
set bardata linedata (keep=origin mpg_city rename=(origin=origin2 mpg_city=mpg_city2));
run;

proc sgplot data=merged;
vbarparm category=origin response=mpg_city / group=type groupdisplay=cluster;
series x=origin2 y=mpg_city2;
run;

I'm not sure of your SAS version; so, if GROUPDISPLAY gives you an error, just remove it and you should still get clustered bars (this default changed when the option was added).

 

Hope this helps!

Dan

View solution in original post


All Replies
Solution
‎11-07-2016 12:09 PM
SAS Super FREQ
Posts: 864

Re: Grouped Bar Chart, Mean line

Currently, there are restrictions that prevent VLINE from working for you in this scenario. However, there is a solution. You can precompute the values in PROC MEAN and use VBARPARM and SERIES to display the result. Here is an example:

 

proc means data=sashelp.cars nway;
class origin type;
var mpg_city;
output out=bardata mean=;
run;

proc means data=sashelp.cars nway;
class origin;
var mpg_city;
output out=linedata mean=;
run;

data merged;
set bardata linedata (keep=origin mpg_city rename=(origin=origin2 mpg_city=mpg_city2));
run;

proc sgplot data=merged;
vbarparm category=origin response=mpg_city / group=type groupdisplay=cluster;
series x=origin2 y=mpg_city2;
run;

I'm not sure of your SAS version; so, if GROUPDISPLAY gives you an error, just remove it and you should still get clustered bars (this default changed when the option was added).

 

Hope this helps!

Dan

New Contributor
Posts: 3

Re: Grouped Bar Chart, Mean line

Hi Dan,

 

I have a follow up now. How can I add a vertical reference line to this chart? I am trying but nothing is showing up.

 

This is my code:

 

proc sgplot data=alldat2&testcd dattrmap=attrmap;

by subject;

vbarparm category=vsdy response=vsstresn / group=vstptnumm groupdisplay=cluster name="bar" attrid=X;
series x=vsdy y=vsmean / lineattrs=(thickness=1) y2axis name="line";

refline 100/axis=x;

yaxis VALUES= (&&min&testcd to 166 by 10) ;
y2axis VALUES=(&&min&testcd to 166 by 10);


format vstptnumm $vstim. vsdy week.;;

run;

SAS Super FREQ
Posts: 864

Re: Grouped Bar Chart, Mean line

[ Edited ]

This ie actually a little tricky, due to the discrete axis and the format. The REFLINE statement knows nothing about the format associated with the "vsdy" variable, so the "100" gets plotted on the end of the discrete axis. Since there is no real data there, the refline gets clipped off of the end (using the NOCLIP option on the REFLINE statement would probably make it appear). However, your desire is to have the line put at week 100, so you need to put that value in a data set where you can associate a format with it. Given the example I gave you earlier, this is probably the simplest way:

 

data merged;
set bardata linedata (keep=origin mpg_city rename=(origin=origin2 mpg_city=mpg_city2)) end=_last_;
if (_last_) then refvar=100;
run;

Now, on your FORMAT statement, add the refvar:

 

format vstptnumm $vstim. vsdy week. refvar week.;

 

and add the variable to the REFLINE statement:

refline refvar / axis=x;

 

Give that a try and see if it works for you.

 

Thanks!

Dan

New Contributor
Posts: 3

Re: Grouped Bar Chart, Mean line

Thank you, this worked!
Post a Question
Discussion Stats
  • 4 replies
  • 173 views
  • 0 likes
  • 2 in conversation