DATA Step, Macro, Functions and more

Us %sysfunc or %substr to create fiscal year

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 101
Accepted Solution

Us %sysfunc or %substr to create fiscal year

I'm doing something on my data files that contain fiscal year values (e.g. 1314, 1415) in the file name, and my %do %to year valus are 4-digit values (e.g. 2014, 2015). How do I create fiscal year values using such as sysfuncs? Here is the sample code. The one I wrote didn't work.

 

data have2014; input id;
datalines;
1
2
3
;
run;

data have2015; set have2014; run;

 

%macro renm;
%do year=2014 %to 2015;
%let fy= %substr(&year,3,2)%sysfunc(putn(%eval(substr(&year,3,2).+1),z2.))
data want&year; set have&fy;
%end;
%mend;
%renm;

 

So the input file names are have1415, have1516, and

I'd like to produce output file names want2014 and 2015.

 

Thanks.


Accepted Solutions
Solution
‎03-03-2017 12:40 PM
Super User
Posts: 5,085

Re: Us %sysfunc or %substr to create fiscal year

Relying on the last statement in your post, here is an example of how to change from have1415 to want2014 (and also from have1516 to want2015).

 

%macro renm;

%local year fy;
%do year=2014 %to 2015;
   %let fy= %substr(&year,3,2);

   %let fy = &fy%sysfunc(putn(%eval(&fy+1),z2));
data want&year; set have&fy;
%end;
%mend;
%renm

 

I can't test it right now, and it looks only mildly different than your original.  In fact, if you had changed SUBSTR to %SUBSTR in your longer statement, it might have worked.  (Removing the dot after z2 probably works either way.)

View solution in original post


All Replies
Solution
‎03-03-2017 12:40 PM
Super User
Posts: 5,085

Re: Us %sysfunc or %substr to create fiscal year

Relying on the last statement in your post, here is an example of how to change from have1415 to want2014 (and also from have1516 to want2015).

 

%macro renm;

%local year fy;
%do year=2014 %to 2015;
   %let fy= %substr(&year,3,2);

   %let fy = &fy%sysfunc(putn(%eval(&fy+1),z2));
data want&year; set have&fy;
%end;
%mend;
%renm

 

I can't test it right now, and it looks only mildly different than your original.  In fact, if you had changed SUBSTR to %SUBSTR in your longer statement, it might have worked.  (Removing the dot after z2 probably works either way.)

Super User
Posts: 17,868

Re: Us %sysfunc or %substr to create fiscal year

%let fy= %substr(&year,3,2)%eval(%substr(&year,3,2)+1);

Super User
Posts: 5,085

Re: Us %sysfunc or %substr to create fiscal year

Reeza,

 

I suspect that would work for the years in the example, but would lose the leading zero when one exists in the second half of the fiscal year.

Super User
Posts: 17,868

Re: Us %sysfunc or %substr to create fiscal year

@Astounding You are correct Smiley Happy

 

This is why I always create my macro variables in a data step rather than macro code.

 

%let fy= %substr(&year,3,2)%sysfunc(putn(%eval(%substr(&year,3,2)+1), z2.));
Frequent Contributor
Posts: 101

Re: Us %sysfunc or %substr to create fiscal year

Thanks a lot. It worked and for pointing the obvious errors I missed and the concatenated line by Reeza:

%let fy= %substr(&year,3,2)%sysfunc(putn(%eval(%substr(&year,3,2)+1), z2.));

 

data have1415; input id;
datalines;
1
2
3
;
run;
data have1516; set have1415; run;

 

%macro renm;
%do year=2014 %to 2015;
%let fy= %substr(&year,3,2)%sysfunc(putn(%eval(%substr(&year,3,2)+1), z2.));
data want&year; set have&fy; run;
%end;
%mend;
%renm;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 158 views
  • 2 likes
  • 3 in conversation