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

I would like to creat a "box-plot like" graph with summary statistical ouptut. I would like to be able to specify the box and whisker source data and wonder if there is ANY way in sgplot to do this?  

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

Yes, use PROC BOXPLOT. There is a standard format for a data set that specifies the values for the box, whiskers, and outliers.  Study this example to see the format of the BOX= data set, which can be used to create a box plot from any summary statistics:

/* SGPLOT version */
proc sgplot data=sashelp.cars;
vbox mpg_city / category=Origin;
run;

/* to use PROC BOXPLOT, sort by the categorical variable */
proc sort data=sashelp.cars out=cars(keep=Origin mpg_city);
by Origin;
run;

/* Use PROC BOXPLOT to output the values for the schematic box plot */
proc boxplot data=cars;
   plot MPG_CITY*Origin / boxstyle=schematic outbox=Box;
run;

proc print data=Box;
run;

/* create box plot from input data set */
proc boxplot box=Box;
   plot MPG_CITY*Origin / boxstyle=schematic;
run;

Here's the doc for PROC BOXPLOT and the input data set: SAS Help Center: Input Data Sets

Although I suggest PROC BOXPLOT, you can also use PROC SGPLOT. See this article: Annotate features of a schematic box plot in SGPLOT - The DO Loop (sas.com)

 

View solution in original post

8 REPLIES 8
Quentin
Super User

I don't see it in SGPLOT.   But it's available in GTL, so you could write the template then use SGRENDER to make the plot.  https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/grstatgraph/p1db7ll5bzyo4nn1pv31g04etput.htm

 

The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.
ballardw
Super User

You might provide some example data in the form of working data step code that want to display and a description of what to do with each value.

 

You can use a POLYGON statement to create a box and and the HighLow to create whiskers. But you are going to have to provide a lot of explicit values in variables.

Multiple plot statements in the same call to Proc Sgplot (or sgpanel) will overlay plots within limits

grsanford
Fluorite | Level 6

Example Figure.jpgI would like to plot something like the attached figure but with the ability to specifiy the parameter that determin box size, mean, and whisker min/max based on an output dataset (see attache example). 

Rick_SAS
SAS Super FREQ

Yes, use PROC BOXPLOT. There is a standard format for a data set that specifies the values for the box, whiskers, and outliers.  Study this example to see the format of the BOX= data set, which can be used to create a box plot from any summary statistics:

/* SGPLOT version */
proc sgplot data=sashelp.cars;
vbox mpg_city / category=Origin;
run;

/* to use PROC BOXPLOT, sort by the categorical variable */
proc sort data=sashelp.cars out=cars(keep=Origin mpg_city);
by Origin;
run;

/* Use PROC BOXPLOT to output the values for the schematic box plot */
proc boxplot data=cars;
   plot MPG_CITY*Origin / boxstyle=schematic outbox=Box;
run;

proc print data=Box;
run;

/* create box plot from input data set */
proc boxplot box=Box;
   plot MPG_CITY*Origin / boxstyle=schematic;
run;

Here's the doc for PROC BOXPLOT and the input data set: SAS Help Center: Input Data Sets

Although I suggest PROC BOXPLOT, you can also use PROC SGPLOT. See this article: Annotate features of a schematic box plot in SGPLOT - The DO Loop (sas.com)

 

grsanford
Fluorite | Level 6

Thank you @Rick_SAS! I ended up studying the box= dataset, formatting my data accordingly, and then using proc sgpanel as I am more familiar with manipulating the visuals in sgpanel. I think that the results turned out fantastic!

 

proc sgpanel data=fig1 noautolegend sganno=anno;
panelby dpt / rows=2 columns=1 novarname uniscale=column;
vbox _value_ / group=sys nomean lineattrs=(color=black) medianattrs=(color=black) whiskerattrs=(color=black) capshape=none;
refline 0 / lineattrs=(color=crimson);
rowaxis label=" " offsetmax=.1;
keylegend / title=" " position=bottom noborder;
run; 

 

new figure.png

Quentin
Super User

As I mentioned, the BOXPLOTPARM statement in GTL allows you to generate a boxplot from summary statistics.

 

So given summary statistics for two treatment groups:

 

data have ;
  input trt stat : $12. value ;
  cards ;
1 min    0
1 Q1     10
1 median 20
1 Q3     30
1 max    40
2 min    20
2 Q1     30
2 median 40
2 Q3     50
2 max    60
;

 

 

You can plot it with : 

proc template;
  define statgraph boxplotparm1;
    begingraph;
      layout overlay;
        boxplotparm y=value x=trt stat=stat;
      endlayout;
    endgraph;
  end;
run;
 
proc sgrender data=have template=boxplotparm1;
run;

And you get:

Quentin_0-1720900374495.png

You can add a legend and coloring by group and all that sort of stuff.

The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.
Ksharp
Super User

I know Rick Quantin have already posted the solution. Here I just would like to show PROC SGPLOT solution to have some fun.

 

 

data have ;
  input trt stat : $12. value ;
  cards ;
1 min    0
1 Q1     10
1 median 20
1 Q3     30
1 max    40
2 min    20
2 Q1     30
2 median 40
2 Q3     50
2 max    60
;
proc transpose data=have out=want;
by trt;
var value;
id stat;
run;

proc sgplot data=want noautolegend;
highlow x=trt low=Q1 high=Q3/type=bar barwidth=0.4 fillattrs=(color=cxBBC2DC) lineattrs=(color=black);
highlow x=trt low=min high=min/type=bar barwidth=0.2 lineattrs=graphdata1 nofill;
highlow x=trt low=max high=max/type=bar barwidth=0.2 lineattrs=graphdata1 nofill;
highlow x=trt low=median high=median/type=bar barwidth=0.4 lineattrs=graphdata1 nofill;
highlow x=trt low=q3 high=max/ lineattrs=graphdata1;
highlow x=trt low=min high=q1/ lineattrs=graphdata1;

xaxis type=discrete;
yaxis label='value';
run;

Ksharp_0-1720926749278.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!

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
  • 8 replies
  • 1069 views
  • 12 likes
  • 5 in conversation