BookmarkSubscribeRSS Feed
BruceBrad
Lapis Lazuli | Level 10

I have some data recorded for financial years with some years missing. I would like to plot this with the x-axis properly scaled. What is the best way to do this? For example, in the program below, the distance between the 2001-02 and 2002-03 tick marks are the same as the distance between the 2002-03 and 2009-10 marks. I would like to scale them appropriately. Is there an axis scaling option that does this?

data test;
input y year $;
datalines;
1 2001-02
5 2002-03
3 2009-10
;
run;
proc sgplot data=test;
  series y=y x=year /markers;
  run;
4 REPLIES 4
ballardw
Super User

Use an actual date value with a format to display the value as desired.

 

data test;
input y date anydtdte.;
format date yymmd7.;
datalines;
1 2001-02
5 2002-03
3 2009-10
;
run;
proc sgplot data=test;
  series y=y x=date /markers;
run;
BruceBrad
Lapis Lazuli | Level 10

Thanks. I wasn't aware of the anydtdte informat.  Unfortunately this produces a plot with '2001   2002' etc on the x-axis rather than '2001-02  2002-03' etc. I need the latter. Is there an output format I can use?

ballardw
Super User

Take a look at the XAXIS statement to control which VALUES that will appear on the axsis and the VALUESFORMAT on how to display the dates. You can use syntax like values = ('01Jan2000'd to '01Jan2011'd by month) to get monthly tick marks. The YYMMd7. format will display dates as yyyy-mm.

 

The range of your example data is such that if you display every month on the axis then your tick marks may get crowded or too busy depending on the current graphics display area. You may need to adjust that with the ODS Graphics/ width= option.

BruceBrad
Lapis Lazuli | Level 10

I need the labels to be "2001-02" etc rather than standard SAS date markers. However, building on the values option, I think the following code does the trick. (I've run the axis out to 2020 to check that the "fitpolicy=thin" option works).

 

It would be more elegant if SAS had an output format that mapped a SAS date to a fiscal year string of this type. That way SAS would recognise that the axis is a date axis and apply defaults accordingly.

 

 

* PlotTest.sas;
data test;
input y year $;
datalines;
1 2001-02
5 2002-03
3 2009-10
;
run;
%macro fyvaluesq(start,end);
%* e.g %fyvaluesq(2001,2004) => 
   "2001-02" "2002-03" "2003-04"
   Used in sgplot for fin year plots;
%let output=;
%do y1 = &start %to %eval(&end-1);
  %let y2 = %substr(%eval(&y1+1),3,2);
  %let output=&output %bquote(")&y1-&y2%bquote(");
%end;
%unquote(&output)
%mend;

proc sgplot data=test;
  series y=y x=year /markers;
  xaxis values=(%fyvaluesq(2001,2020)) fitpolicy=thin;
  run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 722 views
  • 2 likes
  • 2 in conversation