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
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.
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.
Thanks a ton!! This is excellent and worked perfectly
Very kindly appreciated,
Nick
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.
Wow very interesting and not at all something I'd have thought of! Thank you for your help!
Very kindly appreciated,
Nick
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.
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.