DATA Step, Macro, Functions and more

Macro that returns code - help requested

Accepted Solution Solved
Reply
Contributor
Posts: 38
Accepted Solution

Macro that returns code - help requested

Hello all, I am trying to macro-tize a portion of my code (in red) that involves the eta variable.

%let &Nsites=5;

data PHData;

    array SiteDummy{&Nsites} (&Nsites*0);

    array SiteRate{&Nsites} (&Nsites*0);


     eta=log(med/3)*StomaType +

          log(med/SiteRate{2})*SiteDummy{2} +

          log(med/SiteRate{3})*SiteDummy{3} +

          log(med/SiteRate{4})*SiteDummy{4} +

          log(med/SiteRate{5})*SiteDummy{5} +

          log(med/7)*barrier;

run;


Given the number of sites, that portion of code should be dynamic.


For example if &Nsites=1 then it should be "log(med/SiteRate{2})*SiteDummy{2} +"

                   if &Nsites=2 then it should be "log(med/SiteRate{2})*SiteDummy{2} +

                                                                log(med/SiteRate{3})*SiteDummy{3} +"


Can a macro be written so it can be used in the following way:


%let &Nsites=5;

data PHData;

    array SiteDummy{&Nsites} (&Nsites*0);

    array SiteRate{&Nsites} (&Nsites*0);


eta=log(med/3)*StomaType +

    %SiteLinPred(Nsites=&Nsites)

     log(med/7)*barrier;

run;



Thank you




Accepted Solutions
Solution
‎11-13-2014 03:16 PM
Super User
Posts: 17,912

Re: Macro that returns code - help requested

Why a macro? You can use an array loop

eta_temp=0;

do i=1 to &nsites;

eta_temp=log(med/siterate(i)*siteDummy(i))+eta_temp;

end;

eta=log(med/3)*StomaType +

   eta_temp+

     log(med/7)*barrier;

View solution in original post


All Replies
Solution
‎11-13-2014 03:16 PM
Super User
Posts: 17,912

Re: Macro that returns code - help requested

Why a macro? You can use an array loop

eta_temp=0;

do i=1 to &nsites;

eta_temp=log(med/siterate(i)*siteDummy(i))+eta_temp;

end;

eta=log(med/3)*StomaType +

   eta_temp+

     log(med/7)*barrier;

Contributor
Posts: 38

Re: Macro that returns code - help requested

Reeze I never thought about using an array loop! Thanks. I actually figured out how to do it using a macro as well for completeness sake.

Thanks again

%macro SiteLinPred(Nsites=);

    %do i=2 %to &Nsites-1;

        log(med/SiteRate{&i})*SiteDummy{&i} +

    %end;

    log(med/SiteRate{&Nsites})*SiteDummy{&Nsites}

%mend SiteLinPred;

%let &Nsites=5;

data PHData;

    array SiteDummy{&Nsites} (&Nsites*0);

    array SiteRate{&Nsites} (&Nsites*0);


     eta=log(med/3)*StomaType +

    %SiteLinPred(Nsites=&Nsites) +

     log(med/7)*barrier;

run;

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 185 views
  • 0 likes
  • 2 in conversation