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

I have 5 binary predictors that I what to use in 5 simple logistic models, respectively. Though, this code outputs a single multiple logistic model with all 5 predictors in it. Help would be appreciated:

 

%Let variable = X1 X2 X3 X4 X5;
proc logistic data=mydata;
	class &variable (ref='0')		 	
				/ param=ref;
	model Y (event='Yes') = 
				&variable / lackfit FIRTH;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You need to loop through all your variables.

 

See the section on:

A macro program for repeating a procedure multiple times

 

http://www.ats.ucla.edu/stat/sas/seminars/sas_macros_introduction/

 

 

View solution in original post

4 REPLIES 4
Reeza
Super User

You need to loop through all your variables.

 

See the section on:

A macro program for repeating a procedure multiple times

 

http://www.ats.ucla.edu/stat/sas/seminars/sas_macros_introduction/

 

 

data_null__
Jade | Level 19
I would transpose the CLASS variables and use a BY statement in proc logistic; by _NAME_; No macro variables or looping needed.
H
Pyrite | Level 9 H
Pyrite | Level 9

Thanks to both of you. I first quickly went with data_null_'s option, though it ceased because data were not sorted. I imagine that I would need to incorporate a proc sort into the code.

 

I then ended up using the link in the former post, which gave a comparable example, see below. I am going to tweak it to grab other components of the output. 

%macro mylogit2(all_indeps, outest);
  %let k=1;
  %let indep = %scan(&all_indeps, &k);
  %do %while("&indep" NE "");
    title "independent variable is &indep";
    proc logistic data=mydata des outest=_est&k;
     model Y (event='Yes') = &indep;
    run;
    %let k = %eval(&k + 1);
    %let indep = %scan(&all_indeps, &k);
  %end;
  %if "&outest" NE "" %then 
  %do;
    data &outest;
      set 
      %do i = 1 %to &k - 1;
        _est&i
      %end; 
      ;
    run;	  
    %let k = %eval(&k - 1);
    proc datasets;
      delete _est1 - _est&k;
    run;
  %end;
  %else 
  %do;
     %put no dataset name was provided, files are not combined;
  %end;
%mend;
%mylogit2(X1 X2 X3 X4 X5 X6 X7 X8 X9)

%mylogit2(X1 X2 X3 X4 X5 X6 X7 X8 X9, a)
proc print data = a;
  var intercept X1 X2 X3 X4 X5 X6 X7 X8 X9;
run;

 

Reeza
Super User

I highly recommend the transpose method. It makes it easier to capture all your results in one place for comparisons later on.

 

*Generate sample data;
data have;

array x(5) x1-x5;

do Record_ID=1 to 1000;
if rand('bernoulli', 0.3)=1 then Y='Yes';
else Y='No';

do j=1 to 5;
x(j)=rand('bernoulli', 1/j);
end;
output;
end;

drop J;
run;

*Transpose;
proc transpose data=have out=flipped (rename=(_name_=variable col1=Variable_Value));
BY Record_ID Y;
run;

*Resort;
proc sort data=flipped;
by variable;
run;

*Regression;
proc logistic data=flipped;
BY variable;
	class Variable_Value (ref='0')		 	
				/ param=ref;
	model Y (event='Yes') = 
				Variable_Value / lackfit FIRTH;
	ods output parameterestimates=Estimates;
run;

proc print data=Estimates;
run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 3084 views
  • 3 likes
  • 3 in conversation