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

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



1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

2 REPLIES 2
Reeza
Super User

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;

spirto
Quartz | Level 8

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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