BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASdevAnneMarie
Rhodochrosite | Level 12

Hello Experts,

 

Is it possible to create the multiple box plots in SAS. My code seems to be wrong :

proc sgplot data=bdd;
vbox Nmbr_de_lgmnts * nmbr_de_lgmnt_2*Nmbr_de_resid/ displaystats=(mean median datamax min max );
run;

proc boxplot DATA = bdd;
plot Nmbr_de_lgmnts * nmbr_de_lgmnt_2*Nmbr_de_resid/ displaystats=(mean median datamax min max ); 
run;

Thank you for your help !

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

A quick look at the VBOX documentation ought to clear this up.

 

Syntax

VBOX numeric-analysis-variable </options>;
 

 

One analysis variable (singular) is allowed in a VBOX statement. So you would need several runs of PROC SGPLOT to achieve multiple box plots. I leave it up to you to read the PROC BOXPLOT documentation.

--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

A quick look at the VBOX documentation ought to clear this up.

 

Syntax

VBOX numeric-analysis-variable </options>;
 

 

One analysis variable (singular) is allowed in a VBOX statement. So you would need several runs of PROC SGPLOT to achieve multiple box plots. I leave it up to you to read the PROC BOXPLOT documentation.

--
Paige Miller
SASdevAnneMarie
Rhodochrosite | Level 12
Thank you PaigeMiller
A_Kh
Barite | Level 11

It could be looped in macro like this: 

%macro boxit(variables=); 
	%do i=1 %to %sysfunc(countw(&variables)); 
		%let var=%scan(&variables, &i);
		title "Boxplot of &var"; 
		proc boxplot DATA = bdd;
			plot &var/ displaystats=(mean median datamax min max ); 
		run;
	%end; 
%mend;
%boxit(variables=Nmbr_de_lgmnts nmbr_de_lgmnt_2 Nmbr_de_resid); 
Ksharp
Super User

You can change your data structure and using one VBOX statement.

data temp;
set sashelp.class;
obs+1;
run;


proc transpose data=temp out=have;
by obs;
var weight height age;
run;

proc sgpanel data=have;
panelby _name_/layout=panel columns=1 uniscale=column;
vbox col1/ displaystats=(mean median datamax min max ); 
run;

Ksharp_0-1771295026143.png

 

 

Or put them in one PROC SGPLOT:

proc sgplot data=have;
vbox col1/category=_name_ displaystats=(mean median datamax min max ); 
run;

Ksharp_1-1771295138710.png

 

PaigeMiller
Diamond | Level 26

@SASdevAnneMarie 

I think @Ksharp has put his finger on the problem. He said "change your data structure". Judging by your last several posts, and reading between the lines, it seems as if your data is structured poorly for the tasks you want to perform.

 

Long data sets, like the one that @Ksharp produces named HAVE, is a much better format to work from (see Maxim 19). Why? Because SAS PROCs are designed to work on long data sets, and you also the option to use a BY statement to get what you want, or you can use the CATEGORY= in the VBOX statement (and other SGPLOT statements) so that many variables can be plotted without you having to list each variable by name. With the long data set named HAVE in KSharp's program, using PROC TABULATE (in your other recent questions) becomes much much simpler.

 

There's a reason people keep referring you and others to these maxims ... each one of them is good advice that you should follow as much as possible.

 

There's a reason I keep asking lots of people to describe the bigger problem is that we can give you better advice; if you tell us WHY you want to do what you are doing, and WHAT you hope to learn. We can give you better advice if we know the WHY you want to do this, and WHAT you hope to learn and what the bigger problem is that you are trying to solve. People who simply tell us the programming task puts us in a situation where we can't give you as good advice.

--
Paige Miller

Catch up on SAS Innovate 2026

Dive into keynotes, announcements and breakthroughs on demand.

Explore 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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 529 views
  • 2 likes
  • 4 in conversation