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!!!
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.;
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;
You can't have negatives and positives in your names. You can include them in the labels if you want.
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.;
Worked perfectlyI Thanks! 🙂
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.