Output work file named based on macro loop iteration

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Output work file named based on macro loop iteration

Hi there,

I have a double nested macro loop and at the end of each iteration I want to output the resulting work file and at the same time have it named so that I know which iteration of each loop it is at. I can do it so that I have either &I or &J in the name but not both...

Code looks something like this:

 

%MACRO LOOP;
%DO I = 1 %TO 1;
%DO J = 1 %TO 1;

 

*CALCULATIONS AND STEPS IN HERE;

 

DATA HAVE;
SET HAVE;
*CALCULATIONS IN HERE
RUN; QUIT;

 

%END;
%END;
%MEND LOOP;

 

So what I want is multiple output files (using the data I have after each loop) that are named "LOOP_-1_+1", "LOOP_-3_+2" etc...

Thanks!!!


Accepted Solutions
Solution
‎09-28-2017 09:37 PM
Super User
Posts: 5,598

Re: Output work file named based on macro loop iteration

[ Edited ]

Because your loop re-uses HAVE, the easiest way would be to copy over the output at the end of the loop.  Instead of a QUIT statement, replace that with:

 

data loop_&i._&j.;

set have;

run;

 

**********************************

 

EDITED:  An even better idea.

 

Really, you only need to change one line of code.  Right now your program includes:

 

DATA HAVE;

 

Change this line to read:

 

data have loop_&i._&j.;

 

 

View solution in original post


All Replies
Trusted Advisor
Posts: 1,613

Re: Output work file named based on macro loop iteration

Try addapt the macro as:

%MACRO LOOP;
%DO I = 1 %TO 1;
%DO J = 1 %TO 1;
 
*CALCULATIONS AND STEPS IN HERE;

/* calculate previous I, J values */ %let m = &i; %let n = &j; %if &n > 1 %then %let n = %eval(&n - 1); %else %do; %if &i > 1 %then %do; %let n = %TO; %let m = %eval(&i -1); %end; %end; DATA HAVE&i&j; %if &i=1 and &j=1 %then %do; SET HAVE; %end; %else %do; SET HAVE&m&n; %end; *CALCULATIONS IN HERE RUN; QUIT; %END; %END; %MEND LOOP;
Super User
Posts: 20,203

Re: Output work file named based on macro loop iteration

You can't have negatives and positives in your names. You can include them in the labels if you want. 

Solution
‎09-28-2017 09:37 PM
Super User
Posts: 5,598

Re: Output work file named based on macro loop iteration

[ Edited ]

Because your loop re-uses HAVE, the easiest way would be to copy over the output at the end of the loop.  Instead of a QUIT statement, replace that with:

 

data loop_&i._&j.;

set have;

run;

 

**********************************

 

EDITED:  An even better idea.

 

Really, you only need to change one line of code.  Right now your program includes:

 

DATA HAVE;

 

Change this line to read:

 

data have loop_&i._&j.;

 

 

Occasional Contributor
Posts: 6

Re: Output work file named based on macro loop iteration

Posted in reply to Astounding

Worked perfectlyI Thanks! Smiley Happy

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 145 views
  • 0 likes
  • 4 in conversation