DATA Step, Macro, Functions and more

Updating Macro Variable with Macro?

Accepted Solution Solved
Reply
Contributor
Posts: 27
Accepted Solution

Updating Macro Variable with Macro?

I have several programs that I run for several different years to generation reports.  Currently, I have to manually change the %let CYEAR each time I run the program, and I run it for 2010 - 2016. Is there a way for it to run 1 time for all the years I want to run? 

 

%let CYEAR = 2010;  /* YEAR OF COHORT */
%let CYEAR2 = %eval(&CYEAR. + 1);
%let TERM = &CYEAR.FA; /* TERM OF COHORT */
%let NTERM = &CYEAR2.SP; /* +1 TERM TO CHECK 1 TERM RETENTION STATUS */
%let NYTERM = &CYEAR2.FA; /* +1 YEAR TO CHECK 1 YEAR RETENTION STATUS */

Accepted Solutions
Solution
‎03-27-2017 09:48 AM
PROC Star
Posts: 7,473

Re: Updating Macro Variable with Macro?

Posted in reply to laura6728

Don't know without seeing the rest of the code you run, but one possibility would be to wrap everything in a macro. e.g.:

 

%macro doit (first,last)
  %do cyear=&first %to &last;  /* YEAR OF COHORT */
    %let CYEAR2 = %eval(&CYEAR. + 1);
    %let TERM = &CYEAR.FA; /* TERM OF COHORT */
    %let NTERM = &CYEAR2.SP; /* +1 TERM TO CHECK 1 TERM RETENTION STATUS */
    %let NYTERM = &CYEAR2.FA; /* +1 YEAR TO CHECK 1 YEAR RETENTION STATUS */

%include your_actual_program.sas;
%mend doit;

%doit (2010,2016)

Art, CEO, AnalystFinder.com

View solution in original post


All Replies
Super User
Posts: 7,782

Re: Updating Macro Variable with Macro?

Posted in reply to laura6728

Within a macro, you can do

%do cyear = 2010 %to 2016;
/* your code */
%end;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎03-27-2017 09:48 AM
PROC Star
Posts: 7,473

Re: Updating Macro Variable with Macro?

Posted in reply to laura6728

Don't know without seeing the rest of the code you run, but one possibility would be to wrap everything in a macro. e.g.:

 

%macro doit (first,last)
  %do cyear=&first %to &last;  /* YEAR OF COHORT */
    %let CYEAR2 = %eval(&CYEAR. + 1);
    %let TERM = &CYEAR.FA; /* TERM OF COHORT */
    %let NTERM = &CYEAR2.SP; /* +1 TERM TO CHECK 1 TERM RETENTION STATUS */
    %let NYTERM = &CYEAR2.FA; /* +1 YEAR TO CHECK 1 YEAR RETENTION STATUS */

%include your_actual_program.sas;
%mend doit;

%doit (2010,2016)

Art, CEO, AnalystFinder.com

Super User
Posts: 19,792

Re: Updating Macro Variable with Macro?

[ Edited ]
Posted in reply to laura6728

Or use CALL EXECUTE to loop and call the macro 6 times, there are lots of ways to automate a macro loop.

Contributor
Posts: 27

Re: Updating Macro Variable with Macro?

Posted in reply to laura6728

I am sorry everyone, I posted this in the BASE forum and I am using EG. I am going to move this over to the EG board. Sorry, and thanks for the help!

Super User
Posts: 7,782

Re: Updating Macro Variable with Macro?

Posted in reply to laura6728

laura6728 wrote:

I am sorry everyone, I posted this in the BASE forum and I am using EG. I am going to move this over to the EG board. Sorry, and thanks for the help!


Not necessary. It's got more to do with Base SAS code than with EG functionality.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 152 views
  • 0 likes
  • 4 in conversation