Data visualization with SAS programming

Scaling financial years in plots

Reply
Frequent Contributor
Posts: 146

Scaling financial years in plots

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;
Super User
Posts: 13,056

Re: Scaling financial years in plots

Posted in reply to BruceBrad

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;
Frequent Contributor
Posts: 146

Re: Scaling financial years in plots

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?

Super User
Posts: 13,056

Re: Scaling financial years in plots

Posted in reply to BruceBrad

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.

Frequent Contributor
Posts: 146

Re: Scaling financial years in plots

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;
Ask a Question
Discussion stats
  • 4 replies
  • 101 views
  • 2 likes
  • 2 in conversation