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.
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.)
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.)
%let fy= %substr(&year,3,2)%eval(%substr(&year,3,2)+1);
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.
@Astounding You are correct 🙂
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.));
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;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.