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
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;
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;
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!
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;
Awesome! Thank you so much for the help!
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.