BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
nick1990
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

4 REPLIES 4
Astounding
PROC Star

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.

nick1990
Fluorite | Level 6

Thanks a ton!! This is excellent and worked perfectly

 

Very kindly appreciated,

Nick

Reeza
Super User

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

nick1990
Fluorite | Level 6

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

 

Very kindly appreciated,

Nick

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 2939 views
  • 2 likes
  • 3 in conversation