Hello,
I used SAS EG to develop a box plot and have successfully added another variable too.
The issue is:--
--One variable I want Box Plot--showing mean, median, std dev, min and max. I added the box plot. So I am good here.
--Second variable I want a line chart for it. It has to overlay the box plot with it's own value.--I am NOT able to do so. And want your help with it.
Below is the code that I used.
Thanks
data buy(drop=i );
set sashelp.buy;
do i = 1 to 10;
nos_sale = round(rand("Uniform")*10,1); /* u ~ U(0,1) */
new_sale_amount=amount*rand("Uniform")*13;
output;
end;
run;
run;
proc sql;
create table buy2 as
select
date
,max(nos_sale) as nos_sale
,new_sale_amount
from buy
group by date
;quit;
PROC SQL;
CREATE VIEW WORK.SORTTempTableSorted AS
SELECT T.DATE, T.new_sale_amount, T.nos_sale
FROM WORK.BUY2 as T
;
QUIT;
Legend1 FRAME ;
SYMBOL1 INTERPOL=BOX VALUE=CIRCLE
HEIGHT=1 MODE=EXCLUDE
;
SYMBOL2 interpol=join VALUE=dot
HEIGHT=1 MODE=EXCLUDE
;
Axis1 STYLE=1 WIDTH=1 MINOR=NONE;
Axis2 STYLE=1 WIDTH=1 MINOR=NONE;
Axis3 STYLE=34 WIDTH=1 MINOR=NONE;
TITLE;
TITLE1 "Box Plot";
FOOTNOTE;
FOOTNOTE1 "Generated by the SAS System (&_SASSERVERNAME, &SYSSCPL) on %TRIM(%QSYSFUNC(DATE(), NLDATE20.)) at %TRIM(%SYSFUNC(TIME(), TIMEAMPM12.))";
PROC GPLOT DATA=WORK.SORTTempTableSorted
;
PLOT new_sale_amount * DATE/
VAXIS=AXIS1
HAXIS=AXIS2
LVREF=4
CVREF=BLACK
AUTOVREF
LEGEND=LEGEND1
;
PLOT2 nos_sale * DATE / overlay
VAXIS=AXIS3
LEGEND=LEGEND1
;
RUN; QUIT;
TITLE; FOOTNOTE;
GOPTIONS RESET = SYMBOL;
Overlay of Basic Plots like Scatter or Series on VBOX or HBOX is allowed starting with SAS 9.40M3. If you have an older version of SAS, you will get the message you are seeing. GTL template will allow such overlays.
If you want to avoid the effort to learn GTL from scratch, use SGPLOT to create the graph you want with only the VBOX and WITHOUT the SERIES overlay. Then, use the TMPLOUT= option on the proc statement to obtain the generated GTL code. Now, you can edit the code, and add the SERIESPLOT statement to get the additional layers.
Try using SGPLOT instead of GPLOT, it's easier to add overlaying graph types onto one plot.
GPLOT graphics aren't as nice either. This is a really bad example but shows you how it would be done.
proc sgplot data=sashelp.heart;
title "Cholesterol Distribution by Weight Class";
hbox cholesterol / category=weight_status;
series x=weight_status y=mrw / x2axis y2axis;
run;
Thank You for your help.
Here are couple of things that I tried.
1.The code you suggested does not work. Not sure why but I get this error.--ERROR: Attempting to overlay incompatible plot or chart types.
2.I am guessing you want me to try proc template. I agree to it. But I want to join the dots highlighted in the attachement.
proc template;
define statgraph vbarscat;
begingraph;
entrytitle 'Overlay Box Chart and Scatter Plot';
layout overlay /yaxisopts=(linearopts= (viewmin=0 viewmax=80
tickvaluesequence= (start=0 end=80 increment=10)
)
);
boxplot x=date y=new_sale_amount /yaxis=y2;
scatterplot y=nos_sale x=date;
endlayout;
endgraph;
end;
proc sgrender data=buy2 template=vbarscat;
run;
I never mentioned anything about PROC TEMPLATE. I usually avoid that like the plague personally. It's a different language and hard 😞
What version (exactly!) of SAS are you using, ie SAS 9.4 TS1M3
I suspect you're running into the issue of using an older version of SAS.
Overlay of Basic Plots like Scatter or Series on VBOX or HBOX is allowed starting with SAS 9.40M3. If you have an older version of SAS, you will get the message you are seeing. GTL template will allow such overlays.
If you want to avoid the effort to learn GTL from scratch, use SGPLOT to create the graph you want with only the VBOX and WITHOUT the SERIES overlay. Then, use the TMPLOUT= option on the proc statement to obtain the generated GTL code. Now, you can edit the code, and add the SERIESPLOT statement to get the additional layers.
I think this slight modification of your code probably gets a little closer to what you're wanting...
data buy(drop=i ); set sashelp.buy;
do i = 1 to 10;
nos_sale = round(rand("Uniform")*10,1); /* u ~ U(0,1) */
new_sale_amount=amount*rand("Uniform")*13;
output;
end;
run;
proc sql;
create table buy2 as
select
date
,max(nos_sale) as nos_sale
,new_sale_amount
from buy
group by date
;quit;
PROC SQL;
CREATE VIEW WORK.SORTTempTableSorted AS
SELECT T.DATE, T.new_sale_amount, T.nos_sale
FROM WORK.BUY2 as T
;
QUIT;
Legend1 FRAME ;
SYMBOL1 INTERPOL=BOX VALUE=CIRCLE
HEIGHT=1 MODE=EXCLUDE
color=red
;
SYMBOL2 interpol=join VALUE=dot
HEIGHT=1 MODE=EXCLUDE
color=blue
;
Axis1 STYLE=0 WIDTH=1 MINOR=NONE;
Axis2 STYLE=1 WIDTH=1 MINOR=NONE label=none;
Axis3 STYLE=0 WIDTH=1 MINOR=NONE;
TITLE;
TITLE1 "Box Plot";
FOOTNOTE;
FOOTNOTE1 "Generated by the SAS System (&_SASSERVERNAME, &SYSSCPL) on %TRIM(%QSYSFUNC(DATE(), NLDATE20.)) at %TRIM(%SYSFUNC(TIME(), TIMEAMPM12.))";
PROC GPLOT DATA=WORK.SORTTempTableSorted
;
PLOT new_sale_amount*DATE=1/
VAXIS=AXIS1
HAXIS=AXIS2
LVREF=4
CVREF=BLACK
AUTOVREF lvref=33 cvref=gray77
LEGEND=LEGEND1
;
PLOT2 nos_sale*DATE=2 / overlay
VAXIS=AXIS3
LEGEND=LEGEND1
;
RUN;
Thank You Robert and Sanjay.
Both of your solutions worked for me.
Thank you very much.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
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.
Ready to level-up your skills? Choose your own adventure.