BookmarkSubscribeRSS Feed
Helal
Fluorite | Level 6

I am using SGPLOT to create Box Plot graph of age by gender. However, I do need to show the graph for overall age followed by each gender in one graph. Is this possible and if yes, how do I modify the code to create such graph.

proc sgplot data=demog;
vbox age / group= sex;

run;

I also want to show some statistics above the box plot. Stats such as N, Mean (SD), Median (IQR). So the summary stats goes like:

Summary of Age by Gender

N                        Overall           Male    Female

Mean (SD)           

Median (IQR

 

Any help is greatly appreciated

 

Helal

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

1) Do something like this

 

data plot;
   set sashelp.class;
   sex = 'A'; output;
   set sashelp.class;
   output;
run;

proc sgplot data = plot;
   vbox age / group= sex;
run;

2) Use the xaxistable statement.

Helal
Fluorite | Level 6

Hi Peter,

Thank you for quick response. Since I am fairly new to SAS, will xaxistable give me the overall graph followed by breakdown of age by each gender? I ran the code you shared and now I only get the box plot for Age and no breakdown. 

 

Thank you,

 

Helal

ballardw
Super User

@Helal wrote:

Hi Peter,

Thank you for quick response. Since I am fairly new to SAS, will xaxistable give me the overall graph followed by breakdown of age by each gender? I ran the code you shared and now I only get the box plot for Age and no breakdown. 

 

Thank you,

 

Helal


You need to show the code you actually used.

 

Your question does not show any xaxistable code so we can't tell what you may be attempting. If you did a summary step to create additional variables such as mean and IQR, then you need to take another step to do that. Proc summary will create multiple levels of summaries such as;

proc summary data=sashelp.class;
   class sex age;
   var height weight;
   output out=work.summary mean= median= qrange=/autoname;
run;

The level of the output variable _type_ indicates which combinations of the Class variable are on each record.

_typ_ = 0 is overall, 1 is each age, 2 is each sex and 3 is the sex and age combinations.

 

 

Helal
Fluorite | Level 6

Here is the code I am using but can't get summary table to show. I don't think I am using xaxistable correctly:

data demog;
set ref.demog_derived;
data demog1;
set demog;
rename gendertyp=Sex;
rename demogageyears=Age;
run;
data demog2;
set demog1;
sex = 'A'; output;
set demog1;
output;
run;
proc sort data=demog2;
by sex;
run;
proc summary data=demog1;
class sex ;
var age;
output out=work.summary mean= median= qrange=/autoname;
run;
data demog3;
merge demog2 summary;
by sex;
run;

proc sgplot data=demog2;
vbox age / group= sex;
xaxistable age sex;
run;

here is the result: I tried to change sex = 'A' to sex = 'Overall' to show blue graph as Overall but I get O only! and not sure what those 8 are at the top of the graph...perhaps outliers.

SGPlot6.png

I found another codes below that I applied to a different data set. This is exactly what I need the data to be presented except the overall graph and overall summary are missing. 

ods output sgplot=sgplotdata;

proc summary data=auditc nway qntldef=5;
class siteabbrev;
var auditc_tot;
output out=summary(drop=_type_ _freq_) n=N mean=Mean median=Median q1=Q1 q3=Q3 std=STD / noinherit;
run;
proc sort data=auditc out=auditc1;
by siteabbrev;
run;
data auditc2;
merge auditc1 summary;
by siteabbrev;
run;
title;
ods path(prepend) work.templat(update);
proc template;
define statgraph sgdesign;
dynamic x value ;
begingraph;
entrytitle halign=center 'Auditc total by Sites';
/* entryfootnote halign=left 'Type in your footnote...';*/
layout lattice / rowdatarange=data columndatarange=data rowgutter=30 columngutter=30;
layout overlay / xaxisopts=( discreteopts=( tickvaluefitpolicy=splitrotate));
innerMargin / align=top;
blockplot x=x block=n /display=(values label outline) valuehalign=center
labelattrs=graphdatatext valueattrs=graphdatatext;
blockplot x=x block=std /display=(values label outline) valuehalign=center
labelattrs=graphdatatext valueattrs=graphdatatext;
blockplot x=x block=mean /display=(values label outline) valuehalign=center
labelattrs=graphdatatext valueattrs=graphdatatext;
blockplot x=x block=median /display=(values label outline) valuehalign=center
labelattrs=graphdatatext valueattrs=graphdatatext;
blockplot x=x block=q1 /display=(values label outline) valuehalign=center
labelattrs=graphdatatext valueattrs=graphdatatext;
blockplot x=x block=q3 /display=(values label outline) valuehalign=center
labelattrs=graphdatatext valueattrs=graphdatatext;
endInnerMargin;
boxplot y=value x=x;
endlayout;
endlayout;
endgraph;
end;
run;
proc sgrender data=auditc2 template=sgdesign;
dynamic value="AUDITC_TOT" x="'SITEABBREV'n" ;
run;

 

result:

SGPlot7.png

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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