DATA Step, Macro, Functions and more

macro do loop

Reply
Occasional Contributor
Posts: 9

macro do loop

Hello,

 

I am trying to run six, stratified logistic regression models. Those which are sorted/stratified by PA_AC1, PA_AC2 and PA_AC3 have the primary predictor PA_AVG, whereas the predictor for those sorted by NA_AC1, NA_AC2 and NA_AC3 is NA_AVG. This is the (non-working) code I've come up with:

 

%macro mod(var1, var2);

 

%macro nest;

 

%DO i=1 %to 3;

 

proc sort data=work.last;

by &&var1&i;

run;

 

proc logistic data=work.last;

model outcome=&&var2;

by &&var1&i;

run;

 

%end;

 

%mend nest;

 

%mend mod;

%mod (PA_AC, PA_AVG);

%mod (NA_AC, NA_AVG);

 

Thanks in advance for any help!

 

 

 

 

 

 

Super User
Super User
Posts: 7,080

Re: macro do loop

[ Edited ]
Posted in reply to epstewart1110

Make sure to use code insert icons on the editor menu bar to insert text.

Do NOT nest macro definitions. It does not change a macro's definition  in any way to nest the %macro/%mend statements inside of some other macro defintion.  You can nest the EXECUTION of macro by calling a macro from within another macro.

But your program does not appear to actually call the inner macro. In fact it does not appear to need the inner macro.

%macro mod(var1, var2);
%DO i=1 %to 3;
 
proc sort data=work.last;
by &var1.&i;
run;
 
proc logistic data=work.last;
model outcome=&var2.&i;
by &var1.&i;
run;
 
%end;
 
%mend mod;
%mod (PA_AC, PA_AVG);
%mod (NA_AC, NA_AVG);

This will work if you have 4 series of actual dataset variables that have names like PA_AC1 - PA_AC3, PA_AVG1-PA_AVG3, etc.

 

 

Occasional Contributor
Posts: 9

Re: macro do loop

Hi Tom,

 

Great, thanks! That's super helpful. Is it always necessary to put a period after the macro variable and before the &i, if doing a do loop within a macro?

 

Emily

Super User
Super User
Posts: 7,080

Re: macro do loop

Posted in reply to epstewart1110

epstewart1110 wrote:

Hi Tom,

 

Great, thanks! That's super helpful. Is it always necessary to put a period after the macro variable and before the &i, if doing a do loop within a macro?

 

Emily


SAS will see the period as marking the end of the macro variable name. It is not actually needed in this example since &i could not be part of the macro variable name. 

 

But your original program had multiple & in the front and that is significant.

So if X=FRED and I=1 then both "&X&I" and "&X.&I." will resolve to "FRED1".

But if you add more & in the front the SAS will re-scan the string. So &&X&I will first convert to &X1 and then SAS will look for a macro variable named X1 to resolve. 

1631  %let x=FRED;
1632  %let i=1;
1633  %put '&X&I' = "&X&I";
'&X&I' = "FRED1"
1634  %put '&&X&I' = "&&X&I";
WARNING: Apparent symbolic reference X1 not resolved.
'&&X&I' = "&X1"
Super User
Posts: 7,866

Re: macro do loop

Posted in reply to epstewart1110

epstewart1110 wrote:

Hi Tom,

 

Great, thanks! That's super helpful. Is it always necessary to put a period after the macro variable and before the &i, if doing a do loop within a macro?

 

Emily


It's not always necessary, but it is never wrong. Therefore it is considered good practice to always add it.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 4 replies
  • 97 views
  • 0 likes
  • 3 in conversation