BookmarkSubscribeRSS Feed
Caetreviop543
Obsidian | Level 7

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!

 

 

 

 

 

 

4 REPLIES 4
Tom
Super User Tom
Super User

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.

 

 

Caetreviop543
Obsidian | Level 7

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

Tom
Super User Tom
Super User

@Caetreviop543 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"
Kurt_Bremser
Super User

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 758 views
  • 0 likes
  • 3 in conversation