Solved
Contributor
Posts: 71

# Varying the size of a boxplot

I'm using PROC SGPLOT and I'd like to create a box plot with box plots whose widths vary proportionately with the group size.

In PROC BOXPLOT I'm able to do this using boxwidthscale = 1, but can't seem to find an equivalent in PROC SGPLOT.

Does anyone know of any such code that can do this in SGPLOT?

And I'm using SAS 9.3

Thanks!

Accepted Solutions
Solution
‎05-23-2016 01:36 PM
SAS Super FREQ
Posts: 1,226

## Re: Varying the size of a boxplot

[ Edited ]

PROC BOXPLOT uses the GTL BOXPLOTPARM statement to render the box plot.  BOXPLOTPARM supports the BOXWIDTH statistic value, which allows this feature.  PROC SGPLOT does not support this feature, as it is seen to be beyond what is commonly requested.

However, if you are willing write a bit off GTL code, you can do this.  First, run the regular SGPLOT code with ODS OUTPUT SGPLOT=boxdata; statement.  This will save the data needed to plot the boxplot into the "BoxData" data set.  Examine this data set and you will see three computed columns ending with "_X", "_Y" and "_ST".  "_X" is the category variable.  "_ST" is the statistic column which contains the box plot features such as "Q!", "Q3" and so on.  These along with corresponding "_Y" values are used to display the boxes.

BoxPlotParm statement also supports the "BOXWIDTH" statistic, with corresponding value 0.0 - 1.0.  You will need to compute the appropriate value for BoxWidth, and insert it into the data set.  Here I have computed BoxWidth based on N.  See graph and code below.  Value of N is also displayed.

Code

/*--Get Box Plot graph data--*/

ods output sgplot=boxdata;
proc sgplot data=sashelp.cars;
vbox mpg_city / category=type;
run;

/*--Find largest N value--*/
data null;
retain maxN 0;
set boxdata end=last;
if box_mpg_city_x_type_sortorder_st eq 'N' then maxN=max(maxN, BOX_MPG_CITY_X_TYPE_SORTORDER__Y);
if last then call symput("MaxN", maxN);
run;

proc sort data=boxdata;
by BOX_MPG_CITY_X_TYPE_SORTORDER__X;
run;

data boxdataWidth;
set boxdata(where=(BOX_MPG_CITY_X_TYPE_SORTORDER__X ne ''));
by BOX_MPG_CITY_X_TYPE_SORTORDER__X;

if box_mpg_city_x_type_sortorder_st eq 'N' then do;
N=BOX_MPG_CITY_X_TYPE_SORTORDER__Y;
output;
boxwidth=0.05+ 0.5* n/&MaxN;
box_mpg_city_x_type_sortorder_st='BOXWIDTH';
BOX_MPG_CITY_X_TYPE_SORTORDER__Y=boxwidth;
output;
end;
else output;
run;

/*--Define Box Plot template--*/

proc template;
define statgraph BoxWidth;
begingraph;
entrytitle 'Mileage by Type';
layout overlay;
boxplotparm x=BOX_MPG_CITY_X_TYPE_SORTORDER__X y=BOX_MPG_CITY_X_TYPE_SORTORDER__Y
stat=BOX_MPG_CITY_X_TYPE_SORTORDER_ST;
scatterplot x=BOX_MPG_CITY_X_TYPE_SORTORDER__X y=eval(BOX_MPG_CITY_X_TYPE_SORTORDER__Y*0+5) /
markercharacter=n;
endlayout;
endgraph;
end;
run;

/*--Render graph--*/
ods graphics / reset width=5in height=3in imagename='BoxWidth';
proc sgrender data=boxdataWidth template=BoxWidth;
format n 3.0;
run;

All Replies
Solution
‎05-23-2016 01:36 PM
SAS Super FREQ
Posts: 1,226

## Re: Varying the size of a boxplot

[ Edited ]

PROC BOXPLOT uses the GTL BOXPLOTPARM statement to render the box plot.  BOXPLOTPARM supports the BOXWIDTH statistic value, which allows this feature.  PROC SGPLOT does not support this feature, as it is seen to be beyond what is commonly requested.

However, if you are willing write a bit off GTL code, you can do this.  First, run the regular SGPLOT code with ODS OUTPUT SGPLOT=boxdata; statement.  This will save the data needed to plot the boxplot into the "BoxData" data set.  Examine this data set and you will see three computed columns ending with "_X", "_Y" and "_ST".  "_X" is the category variable.  "_ST" is the statistic column which contains the box plot features such as "Q!", "Q3" and so on.  These along with corresponding "_Y" values are used to display the boxes.

BoxPlotParm statement also supports the "BOXWIDTH" statistic, with corresponding value 0.0 - 1.0.  You will need to compute the appropriate value for BoxWidth, and insert it into the data set.  Here I have computed BoxWidth based on N.  See graph and code below.  Value of N is also displayed.

Code

/*--Get Box Plot graph data--*/

ods output sgplot=boxdata;
proc sgplot data=sashelp.cars;
vbox mpg_city / category=type;
run;

/*--Find largest N value--*/
data null;
retain maxN 0;
set boxdata end=last;
if box_mpg_city_x_type_sortorder_st eq 'N' then maxN=max(maxN, BOX_MPG_CITY_X_TYPE_SORTORDER__Y);
if last then call symput("MaxN", maxN);
run;

proc sort data=boxdata;
by BOX_MPG_CITY_X_TYPE_SORTORDER__X;
run;

data boxdataWidth;
set boxdata(where=(BOX_MPG_CITY_X_TYPE_SORTORDER__X ne ''));
by BOX_MPG_CITY_X_TYPE_SORTORDER__X;

if box_mpg_city_x_type_sortorder_st eq 'N' then do;
N=BOX_MPG_CITY_X_TYPE_SORTORDER__Y;
output;
boxwidth=0.05+ 0.5* n/&MaxN;
box_mpg_city_x_type_sortorder_st='BOXWIDTH';
BOX_MPG_CITY_X_TYPE_SORTORDER__Y=boxwidth;
output;
end;
else output;
run;

/*--Define Box Plot template--*/

proc template;
define statgraph BoxWidth;
begingraph;
entrytitle 'Mileage by Type';
layout overlay;
boxplotparm x=BOX_MPG_CITY_X_TYPE_SORTORDER__X y=BOX_MPG_CITY_X_TYPE_SORTORDER__Y
stat=BOX_MPG_CITY_X_TYPE_SORTORDER_ST;
scatterplot x=BOX_MPG_CITY_X_TYPE_SORTORDER__X y=eval(BOX_MPG_CITY_X_TYPE_SORTORDER__Y*0+5) /
markercharacter=n;
endlayout;
endgraph;
end;
run;

/*--Render graph--*/
ods graphics / reset width=5in height=3in imagename='BoxWidth';
proc sgrender data=boxdataWidth template=BoxWidth;
format n 3.0;
run;

🔒 This topic is solved and locked.