Please excuse a newbie question but I've searched far & wide and still haven't figured out how to do this simple thing. I want to create a macro variable for the current year, something like
%Let CurrYr = year(today());
and then use this in a macro to combine SAS files for the last 5 years:
%Macro combine;
%Let FYEAR = &curryr;
%Let PYEAR = %eval(&FYEAR - 1)
Set
%DO i = 1 %TO 5;
aaf.AcademicYear.&PYEAR.&FYEAR;
%Let FYEAR = %eval(&curryr - &i);
%Let PYEAR = %eval(&FYEAR - 1);
%End;
run;
%MEND;
So the first iteration would get the file named AcademicYear20122013 and combine it with files for the previous four years thru AcademicYear20082009.
Thanks for any help.
You have 3 issues I see.
First getting a base year. One way similar to your approach is:
%Let CurrYr = %sysfunc(year("&sysdate"d));
If SAS is restarted daily. "Year" is a data step function that requires a date value and today is a datastep function and can't be called directly in macro assignments. Another approach
data _null_;
length yr $ 4 ;
yr = put(year(today()),f4.0);
call symput("CurrYr",yr);
run;
Second, the line
%Let PYEAR = %eval(&FYEAR - 1)
needs a semicolon at the end;
Third, the line
aaf.AcademicYear.&PYEAR.&FYEAR;
should not have a semicolon. This is the actual text generated by the macro. with the semicolon it will generate
set
name1;
name2;
etc.
Also, you may need an "unused" ; after the %do loop to close the SET statement depending on how you are using the combine macro.
You have 3 issues I see.
First getting a base year. One way similar to your approach is:
%Let CurrYr = %sysfunc(year("&sysdate"d));
If SAS is restarted daily. "Year" is a data step function that requires a date value and today is a datastep function and can't be called directly in macro assignments. Another approach
data _null_;
length yr $ 4 ;
yr = put(year(today()),f4.0);
call symput("CurrYr",yr);
run;
Second, the line
%Let PYEAR = %eval(&FYEAR - 1)
needs a semicolon at the end;
Third, the line
aaf.AcademicYear.&PYEAR.&FYEAR;
should not have a semicolon. This is the actual text generated by the macro. with the semicolon it will generate
set
name1;
name2;
etc.
Also, you may need an "unused" ; after the %do loop to close the SET statement depending on how you are using the combine macro.
BallardW,
Thank you very much for your helpful assistance!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.