Data visualization with SAS programming

Reference line with monyy5. formatted date

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

Reference line with monyy5. formatted date

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;


Accepted Solutions
Solution
‎09-16-2014 04:04 PM
Grand Advisor
Posts: 9,701

Re: Reference line with monyy5. formatted date

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.

View solution in original post


All Replies
Solution
‎09-16-2014 04:04 PM
Grand Advisor
Posts: 9,701

Re: Reference line with monyy5. formatted date

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.

Occasional Contributor
Posts: 15

Re: Reference line with monyy5. formatted date

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';

SAS Super FREQ
Posts: 864

Re: Reference line with monyy5. formatted date

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

Post a Question
Discussion Stats
  • 3 replies
  • 878 views
  • 3 likes
  • 3 in conversation