Data visualization with SAS programming

box plots for several measures

Accepted Solution Solved
Reply
Regular Contributor
Posts: 201
Accepted Solution

box plots for several measures

The following work fine for one measure

PROC SGPLOT DATA=SomeData;
	VBOX SomeMeasure
	/ category = SomeCategory NOOUTLIERS;
	title 'Todo';
RUN; 

Can this easily be extended to work with several measures? I can of course copy/adapt/past or even better use a macro but before I do this just wanted to check with the experts. thanks! 

 


Accepted Solutions
Solution
‎12-12-2017 10:46 AM
Super User
Posts: 13,046

Re: box plots for several measures

Posted in reply to csetzkorn

Reshape the data so that you have a categorical variable holding the name/description of the variable and the value.

One way is to transpose data twice or a data step.

Here's an example with double transposition:

Proc transpose data=sashelp.class out=work.classtrans;
   var height weight age;
run;
proc sort data=work.classtrans;
   by  _name_;
run;
proc transpose data=work.classtrans out=work.classtrans2;
 by  _name_;
 var col:;
run;

proc sgplot data=work.classtrans2;
   hbox col1 / category=_name_;
   label col1 ='Value distribution';
run;

If you have many observations and variables of concern this may be pretty resource intensive and the data step using an array holding the variables of interest and transposing that way while adding a categorical variable to hold the name or description may be better.

 

View solution in original post


All Replies
PROC Star
Posts: 1,190

Re: box plots for several measures

Posted in reply to csetzkorn

If I understand your request correctly, you want to create several VBOX plots based on distinct values of some variable?

 

If yes, do not resort to writing a macro loop. Instead, use by group processing like this

 

proc sort data=sashelp.heart out=heart;
	by sex;
run;

proc sgplot data=heart;
	by sex;
	vbox cholesterol / category=deathcause;
run;
PROC Star
Posts: 1,190

Re: box plots for several measures

[ Edited ]
Posted in reply to csetzkorn

On further inspection of your request, I don't think that is what you mean.

 

Still, I would not use a macro loop. Do something like this

 

data have;
	input SomeData:$50. SomeMeasure:$50. SomeCategory:$50.;
	datalines;
sashelp.class Height Sex
sashelp.iris SepalWidth Species
sashelp.cars MPG_City Type
;

data _null_;
	set have;
	length arg $500;
	arg=cat("proc sgplot data=", SomeData, "; vbox ", SomeMeasure, "/ category = ", SomeCategory, "nooutliers; run;");
	call execute(arg);
run;
Solution
‎12-12-2017 10:46 AM
Super User
Posts: 13,046

Re: box plots for several measures

Posted in reply to csetzkorn

Reshape the data so that you have a categorical variable holding the name/description of the variable and the value.

One way is to transpose data twice or a data step.

Here's an example with double transposition:

Proc transpose data=sashelp.class out=work.classtrans;
   var height weight age;
run;
proc sort data=work.classtrans;
   by  _name_;
run;
proc transpose data=work.classtrans out=work.classtrans2;
 by  _name_;
 var col:;
run;

proc sgplot data=work.classtrans2;
   hbox col1 / category=_name_;
   label col1 ='Value distribution';
run;

If you have many observations and variables of concern this may be pretty resource intensive and the data step using an array holding the variables of interest and transposing that way while adding a categorical variable to hold the name or description may be better.

 

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 191 views
  • 4 likes
  • 3 in conversation