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

Hello, I'm using vbox in sgplot to create a box and whiskers chart. By default, the statistics (displaystats) appear below the chart. Is it possible to move them elsewhere, such as as to the right of the chart? Thanks.

 

My code:

proc sgplot data=ips.allvars2 noautolegend;
vbox &var / spread;
vbox &var / spread fillattrs=(color=green) displaystats = (datamax datamin mean median Q1 Q3);
yaxis display=(nolabel);
run;

 

Output:

NeilH_0-1713893600292.png

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

You want this ?

 

/*
You need to save these statistics firstly.
*/


/***********箱型图***************/
%macro box_plot(dsn=,x=,y=,title=);
ods select none;
ods output sgplot=sgplot1;
proc sgplot data=&dsn.  ;
vbox &y./category=&x. ;
run;
ods select all;

%let dsid=%sysfunc(open(sgplot1));
%let first=%sysfunc(nliteral(%sysfunc(varname(&dsid,1))));
%let second=%sysfunc(nliteral(%sysfunc(varname(&dsid,2))));
%let third=%sysfunc(nliteral(%sysfunc(varname(&dsid,3))));
%let dsid=%sysfunc(close(&dsid));

data temp(rename=(&third.=&x. &first.=_y &second.=_stat));
 set sgplot1;
 _value=round(&first.,0.01);
 if not missing(&second.) and &second. in ('MIN' 'MAX' 'MEDIAN' 'MEAN' 'Q1' 'Q3');
 keep _value &first. &second. &third.;
run;
data want;
 set &dsn. temp;
 if _stat  in ( 'MEDIAN' 'Q1' 'Q3') then _value1=_value;
 if _stat  in ( 'MIN' 'MAX') then _value2=_value;
 if _stat  in ( 'MEAN' ) then _value3=_value;
run;
title  c=black  "&title.";  
proc sgplot data=want noautolegend ;
vbox &y./category=&x.  ;
scatter x=&x. y=_y /discreteoffset=0.25  markerchar=_value1 labelstrip  markercharattrs=(size=8) ;
scatter x=&x. y=_y /discreteoffset=0.15  markerchar=_value2 labelstrip  markercharattrs=(size=8) ;
scatter x=&x. y=_y /discreteoffset=0.08  markerchar=_value3 labelstrip  markercharattrs=(size=8);
yaxis integer;
run;
title;
%mend;

%box_plot(dsn=sashelp.heart,x=sex,y=weight, title=VCSS总分基线和术后1个月的箱型图)

Ksharp_0-1713929289684.png

 

View solution in original post

3 REPLIES 3
ballardw
Super User

@NeilH wrote:

Hello, I'm using vbox in sgplot to create a box and whiskers chart. By default, the statistics (displaystats) appear below the chart. Is it possible to move them elsewhere, such as as to the right of the chart? Thanks.

 

My code:

proc sgplot data=ips.allvars2 noautolegend;
vbox &var / spread;
vbox &var / spread fillattrs=(color=green) displaystats = (datamax datamin mean median Q1 Q3);
yaxis display=(nolabel);
run;

 

 


 

Using the Displaystats option of Vbox I believe the answer is going to be no. There is some rationale for that as VBOX could very well be generating multiple boxes. Trying to move them to the side and getting a useful obvious order would be a pain.

 

Depending on exactly what you mean by "right of the chart" you could use a procedure like Summary to create the values and create and inset statement for the chart which has some positioning options, of create an annotate data set to place text in the graph. Or layout options to have Proc Means/Summary/Tabulate/Report output alongside the graph outside the graph area (but Layout options are limited by ODS destination).

 

If you are looking to do this repeatedly for different variables, as implied by the macro variable, the above may get to be a bit much. It might be easier to reshape the data so the topic (what &var represents like "price of widgets" ) is a variable by reshaping the data to have each topic /value instead of multiple variables. Then use Proc summary to get the values by topic and combine with your data. Proc Sgpanel would let you create a separate graph for each topic using the Panel By and the Inset with Proc Sgpanel will accept variables to place in each graph panel.

NeilH
Obsidian | Level 7

Thank you. I believe your answer will prove very helpful. I haven't used Sgpanel or inset yet, but I came across both in my initial digging around for the issue. You are right that I'm looking to create a collection of charts and associated stats for a number of variables. I'd like the final output to be a series of chart/stat combinations, ideally with the stats to the side of the box and whisker lines rather than right inline with them. I'll look into what you suggest.

Ksharp
Super User

You want this ?

 

/*
You need to save these statistics firstly.
*/


/***********箱型图***************/
%macro box_plot(dsn=,x=,y=,title=);
ods select none;
ods output sgplot=sgplot1;
proc sgplot data=&dsn.  ;
vbox &y./category=&x. ;
run;
ods select all;

%let dsid=%sysfunc(open(sgplot1));
%let first=%sysfunc(nliteral(%sysfunc(varname(&dsid,1))));
%let second=%sysfunc(nliteral(%sysfunc(varname(&dsid,2))));
%let third=%sysfunc(nliteral(%sysfunc(varname(&dsid,3))));
%let dsid=%sysfunc(close(&dsid));

data temp(rename=(&third.=&x. &first.=_y &second.=_stat));
 set sgplot1;
 _value=round(&first.,0.01);
 if not missing(&second.) and &second. in ('MIN' 'MAX' 'MEDIAN' 'MEAN' 'Q1' 'Q3');
 keep _value &first. &second. &third.;
run;
data want;
 set &dsn. temp;
 if _stat  in ( 'MEDIAN' 'Q1' 'Q3') then _value1=_value;
 if _stat  in ( 'MIN' 'MAX') then _value2=_value;
 if _stat  in ( 'MEAN' ) then _value3=_value;
run;
title  c=black  "&title.";  
proc sgplot data=want noautolegend ;
vbox &y./category=&x.  ;
scatter x=&x. y=_y /discreteoffset=0.25  markerchar=_value1 labelstrip  markercharattrs=(size=8) ;
scatter x=&x. y=_y /discreteoffset=0.15  markerchar=_value2 labelstrip  markercharattrs=(size=8) ;
scatter x=&x. y=_y /discreteoffset=0.08  markerchar=_value3 labelstrip  markercharattrs=(size=8);
yaxis integer;
run;
title;
%mend;

%box_plot(dsn=sashelp.heart,x=sex,y=weight, title=VCSS总分基线和术后1个月的箱型图)

Ksharp_0-1713929289684.png

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 832 views
  • 0 likes
  • 3 in conversation