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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

2 REPLIES 2
ballardw
Super User

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.

BTAinRVA
Quartz | Level 8

BallardW,

Thank you very much for your helpful assistance!

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
  • 2 replies
  • 648 views
  • 0 likes
  • 2 in conversation