BookmarkSubscribeRSS Feed
magladde
Calcite | Level 5

Hello everyone, I would like to write a bit of code to run a simple proc logistic for every binary variable I have in a data set. I have a %let statement where I have named all of my binary variables, but how would I go about looping through each variable and calling proc logistic?

 

my current code currently runs all binary variables at the same time.

 

%let binary = var1 var2 var4 var6 var8 var 1;

proc logistic data=work.dataset;
    model outcome = &binary/ clodds=pl clparm=pl;
    title "Model single binary variable against outcome";
run;
3 REPLIES 3
Reeza
Super User

My recommended option:

https://blogs.sas.com/content/iml/2017/02/13/run-1000-regressions.html

 

Another option, very similar example except for PROC REG instead.

https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/

 

Skeleton example for you to modify your code:

https://gist.github.com/statgeek/9603186

 


@magladde wrote:

Hello everyone, I would like to write a bit of code to run a simple proc logistic for every binary variable I have in a data set. I have a %let statement where I have named all of my binary variables, but how would I go about looping through each variable and calling proc logistic?

 

my current code currently runs all binary variables at the same time.

 

%let binary = var1 var2 var4 var6 var8 var 1;

proc logistic data=work.dataset;
    model outcome = &binary/ clodds=pl clparm=pl;
    title "Model single binary variable against outcome";
run;

 

PaigeMiller
Diamond | Level 26

A non-macro solution is given here, which probably runs faster than a macro solution

https://blogs.sas.com/content/iml/2017/02/13/run-1000-regressions.html

 

--
Paige Miller
ghosh
Barite | Level 11

Lots of good suggestions however, if you have along list of variables, they might not fit in a single macro variable.  In that case I would suggest put the list in a dataset and use a call Execute construct to loop through the list.

/* Untested code  */
data varlist;
  input binvar $;
cards;
var1
var2
var4
var6
var8
var9
;

data _null_;
  set varlist;
  call symput('binary',trim(binvar));
  call execute('
	  proc logistic data=work.dataset;
	    model outcome = &binary/ clodds=pl clparm=pl;
	    title "Model single binary variable against outcome";
		run;
  ');
run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 682 views
  • 0 likes
  • 4 in conversation