0
votes

I am running a macro loop that contains macro functions like so:

%macro loop;
%do j=1 %to 1000;
%macro variable;
%end;
%mend;

The macro variable itself has some macro functions enclosed like so:

%macro variable;

%macro rename(x);

proc sql;
create table Renamed&j&x as
select *,

rename1 as rename1,
rename2 as rename2,
...

from rename&j&x
quit;

%rename(1);
%rename(2);
....

%mend;
%mend;

What is the correct syntax for these sticky, nested macro loops and variables? I seem to remember using &&j and &x but I'm getting errors.

The code works if i replace all &j with a 2, so the code is fine, the recursive nature of the loop isn't injecting the variable correctly. TIA.

1
Can you make a reproducible dummy example?Therkel
You generally don't want to redefine a macro within a macro loop. A macro compilation does not resolve the macro variables, it sets up the internal framework of how to resolve and where to place the resolution at macro invocation time. The loop is not recursive -- this is a recursive macro because it invokes itself %macro x(n); %if %sysfunc(mod(&n,500)) = 0 %then %put NOTE: &=n; %x(%eval(&n+1)) %mend; %x(1) It will go until you run out of memory.Richard

1 Answers

0
votes

Make sure to define the macro variables your macro uses as LOCAL.

Take for example a macro variable I you might use as a loop counter. If you do not first declare it as local then if there already exists a macro variable named I SAS will modify that macro variable instead of creating a new local one. Then when your macro stops and the macro that calls it tries to continue the value of &I has changed.

PS Don't nest the macro definitions. That will just cause confusion.