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

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?

 

mmea_0-1610440678665.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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:

 

sgplot.PNG

 

View solution in original post

15 REPLIES 15
PeterClemmensen
Tourmaline | Level 20

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 🙂

mmea
Quartz | Level 8

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

PeterClemmensen
Tourmaline | Level 20

Can you show a portion of your data?

mmea
Quartz | Level 8

My dataset i like this just with many more dates :

DATETIME
04JAN2021same day
04JAN2021same day
05JAN2021same day
05JAN2021same day
05JAN2021next day
05JAN2021next 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;  
PeterClemmensen
Tourmaline | Level 20

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:

 

sgplot.PNG

 

mmea
Quartz | Level 8

thank you - but it says that percent is not found?

mmea
Quartz | Level 8

it works now!!

PeterClemmensen
Tourmaline | Level 20

I'm glad you found your answer 🙂

PeterClemmensen
Tourmaline | Level 20

Please remember to close the thread 🙂

mmea
Quartz | Level 8

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

PeterClemmensen
Tourmaline | Level 20

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?

mmea
Quartz | Level 8

exactly - i tried actually to do grouporder = descending - and it worked?

But if you have a better way?

 

i use SAS 9.4

PeterClemmensen
Tourmaline | Level 20

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:

 

sgplot.PNG

 

mmea
Quartz | Level 8

Thank you!!!!!!!!!

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!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 15 replies
  • 1631 views
  • 2 likes
  • 2 in conversation