The codes shown below repeat univariate logsitic regression with the same outcome variable status and different predictor variables (age, sex, race, service, …, one at a time).
lapply() was used to loop over predictor names. By this method, the formula in the result object will be meaningful.
lapply(c("age","sex","race","service","cancer","renal","inf","cpr","sys","heart","prevad","type","frac","po2","ph","pco2","bic","cre","loc"),
function(var) {
formula <- as.formula(paste("status ~", var))
res.logist <- glm(formula, data = icu, family = binomial)
summary(res.logist)
})
Macro was used to loop over variable names.
Reference: http://www.sas.com/offices/europe/uk/support/sas-hints-tips/ht1_jan04.html
/*Looping over variables to perform multiple univariate logistic regression*/
%macro logistrepeat(values);
%local i j ; %* # i for variable name, j for position;
%let j = 1 ;
%do %while(%scan(&values, &j) ne ) ; %* # repeat while jth value is NotEqual empty ;
%let i = %scan(&values, &j) ; %* # Insert jth element to variable i;
/* Logistic regression procedure below */
Title "Model with -- &i -- as predictor";
proc logist descending data= icu;
model status = &i; %* # Variable name inserted here;
run;
%let j = %eval(&j+1) ; %* # Increase the value of j by 1;
%end ;
%mend ;
/* Run the macro */
%logistrepeat(age sex race service cancer renal inf cpr sys heart prevad type frac p02 ph pc02 bic crit loc);