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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 786 views
  • 0 likes
  • 4 in conversation