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