DATA Step, Macro, Functions and more

Macro to Combine Files

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 126
Accepted Solution

Macro to Combine Files

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.


Accepted Solutions
Solution
‎07-08-2013 11:55 AM
Super User
Posts: 10,552

Re: Macro to Combine Files

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.

View solution in original post


All Replies
Solution
‎07-08-2013 11:55 AM
Super User
Posts: 10,552

Re: Macro to Combine Files

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.

Frequent Contributor
Posts: 126

Re: Macro to Combine Files

BallardW,

Thank you very much for your helpful assistance!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 163 views
  • 0 likes
  • 2 in conversation