DATA Step, Macro, Functions and more

How can I write a macro to execute iterative regressions?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

How can I write a macro to execute iterative regressions?

Hi,

I have a group of 10 exposure variables and a group of 10 outcome variables.  I would like to write a macro to regress each outcome variable on each exposure variable (a total of 100 regressions) using GEE with logistic regression, but I am unsure of how to code this.  Any help would be much appreciated!

Thanks!


Accepted Solutions
Solution
‎09-12-2012 04:31 PM
Super Contributor
Posts: 1,636

Re: How can I write a macro to execute iterative regressions?

Hi,

first put your variables in a dataset.  example:

data have;
input (var1 var2) ($1);
cards;
a b
c d
e f
g h
i j
k l
m n
o p
q r
s t
;

data _null_;
set have;
call symputx(cats('dep',_n_),var1);
call symputx(cats('indvars',_n_),var2);
run;

%macro preliminary_gee();
%do i=1 %to 10;
    %do j=1 %to 10;
    proc genmod data=xxx descending;
    class &&indvars&j pharmid studyarm;
    model &&dep&i = &&indvars&j studyarm / dist=bin link=logit type3 wald;
    repeated subject = pharmid / type=cs;
    run;
   %end; %end;
%mend preliminary_gee;

%preliminary_gee()

View solution in original post


All Replies
Super User
Posts: 17,819

Re: How can I write a macro to execute iterative regressions?

See about halfway down the page here, though the whole page is useful if you're just starting out with macros.

https://www.ats.ucla.edu/stat/sas/seminars/sas_macros_introduction/default.htm

You can search for:

A macro program for repeating a procedure multiple times

Occasional Contributor
Posts: 8

Re: How can I write a macro to execute iterative regressions?

Thanks so much for this link!  After reading the page, I was able to create a macro to regress several outcomes on a single predictor, but I would like to regress all of the outcomes on all of the predictors as well.  Are there any more resources you could suggest? 

Super Contributor
Posts: 1,636

Re: How can I write a macro to execute iterative regressions?

Hi,

post your basic code here, we will help you to create a macro.

Occasional Contributor
Posts: 8

Re: How can I write a macro to execute iterative regressions?

Here is the code I am working with so far:

proc genmod data=xxx descending;

    class exposure pharmid studyarm (ref='0') / param=ref;

    model outcome = exposure studyarm / dist=bin link=logit type3 wald;

    repeated subject = pharmid / type=cs;

run;

I would like to create a macro that will regress each of 10 outcome variables on each of 10 exposure variables using this code.  As I mentioned above, I was able to write a macro that would regress each outcome on a given group of exposure variables, but I haven't figured out how to loop the macro through all of the exposure variables individually.  Here is what I have so far:

%macro preliminary_gee(all_deps, indvars =);

  %let k=1;

  %let dep = %scan(&all_deps, &k);

  %do %while(&dep NE);

    proc genmod data=xxx descending;

    class &indvars pharmid studyarm;

    model &dep = &indvars studyarm / dist=bin link=logit type3 wald;

    repeated subject = pharmid / type=cs;

    run;

    %let k = %eval(&k + 1);

    %let dep = %scan(&all_deps, &k);

  %end;

%mend preliminary_gee;

In addition, it would be great to suppress the output and to just create a dataset containing the OR estimates from each regression.  Again, any ideas you have would be much appreciated.

Thanks!

Super User
Posts: 17,819

Re: How can I write a macro to execute iterative regressions?

Nice! But why loop over only the dependents, why not add another do loop and nest over your independents as well?

If you got this far, I think you can get it, but I'm sure ppl here will help regardless. 

Respected Advisor
Posts: 3,777

Re: How can I write a macro to execute iterative regressions?

I don't think JMDECU should do it that way at all.  I would "transform" the data such that the independents and dependents are levels of BY variables and use ONE call to PROC GENMOD.

I don't know exactly which output you want from GENMOD but you can figure that out.

data sample;
   array y[3];
   array x[3];
   do pharmid = 1 to 10;
      studyarm = rantbl(
12345,.5);
      do _n_ = 1 to dim(y);
         y[_n_] = rantbl(0,.5);
         x[_n_] = rannor(0);
         end;
     
output;
     
end;
  
run;
proc print;
  
run;
proc transpose data=sample out=sample2(rename=(col1=Y)) name=dep;
   by pharmid studyarm X:;
   var y:;
   run;
proc transpose data=sample2 out=sample3(rename=(col1=X)) name=ind;
   by pharmid studyarm dep y;
   run;
proc sort data=sample3;
   by dep ind;
   run;
proc print;
  
run;
ods select none;
ods output GEEEmpPEst=GEEEmpPEst;
proc genmod data=sample3 descending;
  
by dep ind;
   class pharmid studyarm;
   model y = x studyarm / dist=bin link=logit type3 wald;
  
repeated subject = pharmid / type=cs;
   run;
ods select all;
proc print data=GEEEmpPEst;
   run;

Message was edited by: data _null_

Solution
‎09-12-2012 04:31 PM
Super Contributor
Posts: 1,636

Re: How can I write a macro to execute iterative regressions?

Hi,

first put your variables in a dataset.  example:

data have;
input (var1 var2) ($1);
cards;
a b
c d
e f
g h
i j
k l
m n
o p
q r
s t
;

data _null_;
set have;
call symputx(cats('dep',_n_),var1);
call symputx(cats('indvars',_n_),var2);
run;

%macro preliminary_gee();
%do i=1 %to 10;
    %do j=1 %to 10;
    proc genmod data=xxx descending;
    class &&indvars&j pharmid studyarm;
    model &&dep&i = &&indvars&j studyarm / dist=bin link=logit type3 wald;
    repeated subject = pharmid / type=cs;
    run;
   %end; %end;
%mend preliminary_gee;

%preliminary_gee()

Occasional Contributor
Posts: 8

Re: How can I write a macro to execute iterative regressions?

Hi Linlin,

Thanks so much for sending me this code.  It worked!  Just one last question - is it possible to create a single output dataset containing the odds ratio estimates generated by each regression?  I know it is possible to do this when using proc logistic, but I am not sure about proc genmod.

Thanks again!  

Regular Contributor
Posts: 184

Re: How can I write a macro to execute iterative regressions?

Two ways.

1. If you persist with the macro loop approach, include a step before the loop to initialize a zero-row target data set. After the PROC GENMOD step but inside the inner loop, include a PROC APPEND.

2. If you adopt data_null_'s suggestion to ditch the macro looping and employ BY processing instead, you should see the stats automatically accumulate in one data set.

jmdecu wrote:

Hi Linlin,

Thanks so much for sending me this code.  It worked!  Just one last question - is it possible to create a single output dataset containing the odds ratio estimates generated by each regression?  I know it is possible to do this when using proc logistic, but I am not sure about proc genmod.

Thanks again!  

☑ This topic is SOLVED.

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

Discussion stats
  • 9 replies
  • 2582 views
  • 6 likes
  • 5 in conversation