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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 1104 views
  • 2 likes
  • 3 in conversation