DATA Step, Macro, Functions and more

Macro to loop through numerous DVs in regression models

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Macro to loop through numerous DVs in regression models

Hi all, I'm sorry if this has been answered before but here's my question:

 

I have 26 outcome variables and need to run a logstic regression with each one as the dependent variable in its own model in order to observe the odds ratios for each outcome.

 

So to simplify my code, I've got something like this:

 

%let out1=a;

%let out2=b;

%let out3=c;

...

%let out26=z;

 

/*out1*/

proc logistic data=&dset;

  class &exposure(param=ref ref='0');

  model &out1(event='1') = &exposure;

  title "&out1 crude";

run; title;

/*out2*/

proc logistic data=&dset;

  class &exposure(param=ref ref='0');

  model &out2(event='1') = &exposure;

  title "&out2 crude";

run; title;

 

And basically have just been copy pasting this 26 times and changing out1, out2, out3, etc in order to get the results. I'm wondering if there's any way with macros / do loops to simply write something like:

 

do i = 1 to 26;

/*all outcomes*/

proc logistic data=&dset;

  class &exposure(param=ref ref='0');

  model &out(i)(event='1') = &exposure;

  title "&out(i) crude";

run; title;

 

This of course doesn't work, and so I'm wondering if someone knows of a solution here?

 

Thanks so much!

-Nick


Accepted Solutions
Solution
‎07-20-2017 12:36 PM
Super User
Posts: 5,513

Re: Macro to loop through numerous DVs in regression models

Nick,

 

While this can be done, you have to realize that macro language will not cut down on the time it takes to run.  It will cut down on the typing, and the chance of making a mistake.

 

Expect that you will call the macro specifying all the information:

 

%run26 (dset=some_data_set_name, exposure=some_variable_name, out_list=a b c d e ... x y z)

 

So you don't get out of typing all the variable names, but you still get out of typing the repetitive parts.  Here's a way to define such a macro:

 

%macro run26 (dset=, exposure=, out_list=);

   %local i next_name;

   %do i=1 %to %sysfunc(countw(&out_list));

         %let next_name = %scan(&out_list, &i);

 

proc logistic data=&dset;

  class &exposure(param=ref ref='0');

  model &next_name (event='1') = &exposure;

  title "&next_name crude";

run; title;

 

   %end;

 

%mend run26;

 

You don't actually have to use 26 variables ... the macro counts how many names appear in your list.

View solution in original post


All Replies
Solution
‎07-20-2017 12:36 PM
Super User
Posts: 5,513

Re: Macro to loop through numerous DVs in regression models

Nick,

 

While this can be done, you have to realize that macro language will not cut down on the time it takes to run.  It will cut down on the typing, and the chance of making a mistake.

 

Expect that you will call the macro specifying all the information:

 

%run26 (dset=some_data_set_name, exposure=some_variable_name, out_list=a b c d e ... x y z)

 

So you don't get out of typing all the variable names, but you still get out of typing the repetitive parts.  Here's a way to define such a macro:

 

%macro run26 (dset=, exposure=, out_list=);

   %local i next_name;

   %do i=1 %to %sysfunc(countw(&out_list));

         %let next_name = %scan(&out_list, &i);

 

proc logistic data=&dset;

  class &exposure(param=ref ref='0');

  model &next_name (event='1') = &exposure;

  title "&next_name crude";

run; title;

 

   %end;

 

%mend run26;

 

You don't actually have to use 26 variables ... the macro counts how many names appear in your list.

Occasional Contributor
Posts: 6

Re: Macro to loop through numerous DVs in regression models

Posted in reply to Astounding

Thanks a ton!! This is excellent and worked perfectly

 

Very kindly appreciated,

Nick

Super User
Posts: 19,829

Re: Macro to loop through numerous DVs in regression models

You can also transpose the data to a long format and then use a BY statement. You may have to combine variables to get exactly what you want. It should be quicker than the multiple calls and the results can be appended into one data set with this method.

 

https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-run-multiple-regres...

Occasional Contributor
Posts: 6

Re: Macro to loop through numerous DVs in regression models

Wow very interesting and not at all something I'd have thought of! Thank you for your help!

 

Very kindly appreciated,

Nick

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 96 views
  • 2 likes
  • 3 in conversation