Programming the statistical procedures from SAS

Macro Variable List for Proc Logistic

Accepted Solution Solved
Reply
Frequent Contributor
Frequent Contributor
Posts: 109
Accepted Solution

Macro Variable List for Proc Logistic

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;

Accepted Solutions
Solution
‎12-18-2015 03:05 PM
Grand Advisor
Posts: 16,940

Re: Macro Variable List for Proc Logistic

[ Edited ]

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


All Replies
Solution
‎12-18-2015 03:05 PM
Grand Advisor
Posts: 16,940

Re: Macro Variable List for Proc Logistic

[ Edited ]

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/

 

 

Respected Advisor
Posts: 3,773

Re: Macro Variable List for Proc Logistic

I would transpose the CLASS variables and use a BY statement in proc logistic; by _NAME_; No macro variables or looping needed.
Frequent Contributor
Frequent Contributor
Posts: 109

Re: Macro Variable List for Proc Logistic

[ Edited ]

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;

 

Grand Advisor
Posts: 16,940

Re: Macro Variable List for Proc Logistic

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;
☑ This topic is SOLVED.

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

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