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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
DanH_sas
SAS Super FREQ

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

4 REPLIES 4
Jay54
Meteorite | Level 14

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;

GordonC
Calcite | Level 5

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!

DanH_sas
SAS Super FREQ

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;

GordonC
Calcite | Level 5

Awesome! Thank you so much for the help!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 9680 views
  • 0 likes
  • 3 in conversation