Data visualization with SAS programming

How to make boxplot for multiple variables with subgroups in SAS?

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

How to make boxplot for multiple variables with subgroups in SAS?

Hi,

 

I have a dataset "baseline" with continuous variables a, b, c, d and e. And I also have a binary variable age (young, old). I would like to create a plot that contains boxplots for a, b, c, d and e within each group of age. Originally, my code looks like:

 

proc template;
define statgraph boxplot_age;
begingraph;
layout overlay / xaxisopts=(display=(ticks tickvalues))
yaxisopts=(griddisplay=on) cycleattrs=true;
boxplot x=age y=a /discreteoffset=-0.3 boxwidth=0.1
name='a' legendlabel='a';
boxplot x=age y=b / discreteoffset= -0.15 boxwidth=0.1
name='b' legendlabel='b';
boxplot x=age y=c / discreteoffset=0 boxwidth=0.1
name='b' legendlabel='c';
boxplot x=age y=d / discreteoffset= 0.15 boxwidth=0.1
name='d' legendlabel='d';
boxplot x=age y=e / discreteoffset= 0.3 boxwidth=0.1
name='e' legendlabel='e';
discretelegend 'a' 'b' 'c' 'd' 'e';
endlayout;
endgraph;
end;
run;

proc sgrender data=baseline name=boxplot_age template=boxplot_age;
run;

 

 

However, what I actually want is to view the value of each variable by age group side by side. For example, I would want a boxplot for varible "a", age=young followed by a boxplot for variable "a", age=old and then followed by a boxplot for variable "b", age=young and so on.

 

 

Please let me know if this is even possible to do in SAS. Any comment would be appreciated and let me know if my question is not clear.

 

Many thanks!

Gordon

 


Accepted Solutions
Solution
‎12-09-2015 04:03 PM
SAS Super FREQ
Posts: 889

Re: How to make boxplot for multiple variables with subgroups in SAS?

I think the best approach to handle this situation is to transpose your data such that the column names are values in a column. A simple examples is below. In your case, the "a-e" variable would be used in place of "weight" and "height", and you would use your "age" variable instead of "sex". 

 

Hope this helps!

Dan

 

data class;
length cat $ 6;
set sashelp.class;
cat="weight"; value=weight; output;
cat="height"; value=height; output;
run;

 

proc sgplot data=class;
vbox value / category=cat group=sex groupdisplay=cluster;
run;

View solution in original post


All Replies
SAS Super FREQ
Posts: 1,080

Re: How to make boxplot for multiple variables with subgroups in SAS?

If you have SAS 9.3, you can use the SGPLOT procedure with a VBOX statement grouped by Age (binary varialbe).. Isuggest transpose your data so a, b, c, d and e become a category, with their values in a variable called Value.  YOu already have Age.  Plot VBOX value / category=cat group=age;

New Contributor
Posts: 3

Re: How to make boxplot for multiple variables with subgroups in SAS?

I see what you mean. It is a great idea. Just to be clear, I only transpose a,b,c,d and e so that I will have a "catagory" variable and a "value" variable. I also have age for each subject so I can merge "age" to the transposed data by subject id. And finially my code will look like:

 

proc sgplot data=baseline_t;
vbox value / category=cat group=age ;
run;

 

I hope It is correct!

Solution
‎12-09-2015 04:03 PM
SAS Super FREQ
Posts: 889

Re: How to make boxplot for multiple variables with subgroups in SAS?

I think the best approach to handle this situation is to transpose your data such that the column names are values in a column. A simple examples is below. In your case, the "a-e" variable would be used in place of "weight" and "height", and you would use your "age" variable instead of "sex". 

 

Hope this helps!

Dan

 

data class;
length cat $ 6;
set sashelp.class;
cat="weight"; value=weight; output;
cat="height"; value=height; output;
run;

 

proc sgplot data=class;
vbox value / category=cat group=sex groupdisplay=cluster;
run;

New Contributor
Posts: 3

Re: How to make boxplot for multiple variables with subgroups in SAS?

Awesome! Thank you so much for the help!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 500 views
  • 0 likes
  • 3 in conversation