I have this code for a SGPLOT:
PROC SGPLOT DATA = DATA PCTLEVEL=GROUP NOBORDER NOWALL ;
format DATE ddmmyyd10.;
VBAR DATE/ GROUP = TIME stat= percent NOOUTLINE grouporder= data ;
Title1 font = 'Calibri' height=16pt "TEST";
keylegend / title="";
XAXIS DISPLAY=(NOLABEL);
YAXIS DISPLAY=(NOLABEL);
styleattrs DATACOLORS=(LightBlue STEEL MistyRose FireBrick);
RUN;
i wish to show a line from the y-axis at 80% on the plot, like this? How can I do that?
There are several ways to do this.
I would probably go this way
data test;
input DATE $9. TIME $ 10-26;
datalines;
04JAN2021 same day
04JAN2021 same day
05JAN2021 same day
05JAN2021 same day
05JAN2021 2 days after
05JAN2021 2 days after
05JAN2021 next day
05JAN2021 next day
05JAN2021 more than 2 days
05JAN2021 more than 2 days
;
proc sort data=test;
by date;
run;
proc freq data = test noprint;
by date;
tables time / out = temp;
run;
proc sql;
create table plot as
select * from temp
order by date, whichc(time, "same day", "next day", "2 days after", "more than 2 days");
quit;
proc sgplot data = plot;
vbarparm category=date response=percent / group=time groupdisplay=stack grouporder=data;
refline 80 / lineattrs=(thickness=5 color=black);
run;
Result:
I'm guessing you already checked out the Refline Statement?
While the Refline Statement can not be used with Vbar, it can be used with VbarParm. See the chart here to see Compatible Statements in Proc SGPLOT.
Try using Vbarparm and Refline together 🙂
The vbarparm dosent work with my data.
in vbar I use dates as variable and my group is a string/labels
in vbarparm you have to have a numeric variable?
or am i wrong
Can you show a portion of your data?
My dataset i like this just with many more dates :
DATE | TIME |
04JAN2021 | same day |
04JAN2021 | same day |
05JAN2021 | same day |
05JAN2021 | same day |
05JAN2021 | next day |
05JAN2021 | next day |
PROC SGPLOT DATA = TEST PCTLEVEL=GROUP NOBORDER NOWALL ;
format DATE ddmmyyd10.;
VBAR DATE/ GROUP = TIME stat= percent NOOUTLINE grouporder= data ;
Title1 font = 'Calibri' height=16pt "test";
keylegend / title="";
XAXIS DISPLAY=(NOLABEL);
YAXIS DISPLAY=(NOLABEL);
styleattrs DATACOLORS=(LightBlue STEEL MistyRose FireBrick);
RUN;
Takes a little pre-work, but see if you can use this as a template
data test;
input DATE $9. TIME $ 10-18;
datalines;
04JAN2021 same day
04JAN2021 same day
05JAN2021 same day
05JAN2021 same day
05JAN2021 next day
05JAN2021 next day
;
proc sort data=test;
by date;
run;
proc freq data = test noprint;
by date;
tables time / out = temp;
run;
proc sgplot data = temp;
vbarparm category=date response=percent / group=time groupdisplay=stack;
refline 80 / lineattrs=(thickness=5 color=black);
run;
Result:
thank you - but it says that percent is not found?
it works now!!
I'm glad you found your answer 🙂
Please remember to close the thread 🙂
I am just curious if I can change the order of my legend.
I have the labels "same day", "next day", "2 days after", "more than 2 days"
I wish to have that same order - but it mixes up in the plot.
I have sorted by date and also tried with time
What version of SAS do you use?
So you want them in that order in the Legend as well as in the actual plot, correct? With 'Same Day' in the bottom and so forth?
exactly - i tried actually to do grouporder = descending - and it worked?
But if you have a better way?
i use SAS 9.4
There are several ways to do this.
I would probably go this way
data test;
input DATE $9. TIME $ 10-26;
datalines;
04JAN2021 same day
04JAN2021 same day
05JAN2021 same day
05JAN2021 same day
05JAN2021 2 days after
05JAN2021 2 days after
05JAN2021 next day
05JAN2021 next day
05JAN2021 more than 2 days
05JAN2021 more than 2 days
;
proc sort data=test;
by date;
run;
proc freq data = test noprint;
by date;
tables time / out = temp;
run;
proc sql;
create table plot as
select * from temp
order by date, whichc(time, "same day", "next day", "2 days after", "more than 2 days");
quit;
proc sgplot data = plot;
vbarparm category=date response=percent / group=time groupdisplay=stack grouporder=data;
refline 80 / lineattrs=(thickness=5 color=black);
run;
Result:
Thank you!!!!!!!!!
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.