Hi,
I'm using sgplot to create a vertical bar chart with a monyy5. formatted date as the X axis. I need to include a vertical reference line on one date value but I'm having trouble getting the line to show up correctly. Below is some test code to recreate the problem. Any help is much appreciated!
Thanks,
John
/* create date values */
data test;
do date = today()-30 to today();
date2 = date;
output;
end;
format date2 monyy5.;
run;
proc print data=test(obs=5);
run;
/* plot unformatted */
title 'Not formatted - refline shows up';
proc sgplot data=test;
vbar date;
refline '01SEP2014'd / axis=x labelloc=outside lineattrs=(color=red) label='TEST';
run;
/* plot MONYY5. formatted */
title 'MONYY5. format - refline does not show';
title2 'And the label is on the wrong month';
proc sgplot data=test;
vbar date2;
refline '01SEP2014'd / axis=x labelloc=outside lineattrs=(color=red) label='TEST';
run;
title;
Use:
refline 'Sep14' / axis=x labelloc=outside lineattrs=(color=red) label='TEST' ;
in the second plot. References must appear as the FORMATTED value, not the value.
A better example would have been as then you are using same variable and values to display with the only difference the format:
proc sgplot data=test;
vbar date;
format date date9.;
refline '01SEP2014'd / axis=x labelloc=outside lineattrs=(color=red) label='TEST';
run;
See what happens when you use the option NOCLIP in the refline for more entertainment.
You might want to change the datastep to have:
do date = '15AUG2014'd to '16SEP2014'd;
so that the data stays the same when the program is run on different dates.
One would expect problems with the refline in September when the program is run in December for example due to clipping of the axis.
Use:
refline 'Sep14' / axis=x labelloc=outside lineattrs=(color=red) label='TEST' ;
in the second plot. References must appear as the FORMATTED value, not the value.
A better example would have been as then you are using same variable and values to display with the only difference the format:
proc sgplot data=test;
vbar date;
format date date9.;
refline '01SEP2014'd / axis=x labelloc=outside lineattrs=(color=red) label='TEST';
run;
See what happens when you use the option NOCLIP in the refline for more entertainment.
You might want to change the datastep to have:
do date = '15AUG2014'd to '16SEP2014'd;
so that the data stays the same when the program is run on different dates.
One would expect problems with the refline in September when the program is run in December for example due to clipping of the axis.
Thanks!
Both solutions worked great (looks like I can only mark a single reply as "Correct Answer"):
refline 'Sep14' / axis=x labelloc=outside lineattrs=(color=red) label='TEST' ;
and
refline myreflines / axis=x labelloc=outside lineattrs=(color=red) label='TEST';
Unfortunately, literal REFLINE values do not pick up formats from the axis and you currently cannot set a format for a literal REFLINE value. However, you CAN work around this by putting your REFLINEs in a data column, setting your format on that column and referring to that column from the REFLINE statement:
refline myreflines / axis=x labelloc=outside lineattrs=(color=red) label='TEST';
Hope this helps!
Dan
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.