BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Solph
Pyrite | Level 9

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

5 REPLIES 5
Astounding
PROC Star

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.)

Reeza
Super User

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

Astounding
PROC Star

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.

Reeza
Super User

@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.));
Solph
Pyrite | Level 9

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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