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

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!!!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

4 REPLIES 4
Shmuel
Garnet | Level 18

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;
Reeza
Super User

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

Astounding
PROC Star

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.;

 

 

Tyler_G
Fluorite | Level 6

Worked perfectlyI Thanks! 🙂

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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