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!
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.