BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
einstein
Quartz | Level 8

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! 

1 ACCEPTED SOLUTION

Accepted Solutions
Jay54
Meteorite | Level 14

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.

 

BoxWidth2.png

 

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;

 

/*--Add BoxWidth stat to data--*/

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;

View solution in original post

1 REPLY 1
Jay54
Meteorite | Level 14

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.

 

BoxWidth2.png

 

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;

 

/*--Add BoxWidth stat to data--*/

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 2549 views
  • 0 likes
  • 2 in conversation