BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ammarhm
Lapis Lazuli | Level 10

Hi all

I have a code that looks like this:

%let var=1;

proc phreg data=have ;

strata country;

class sex ;

model time*event&var(0)=factor&var age weight / rl;

ods output parameterestimates = CorrelationEstimate&var;

run;


The reason why I am using a macro variable is that I need to repeat this code 20 times, with the "var" value from 1 to 20.

I have been doing this manually by changing the number and re-running the code. There should however be a way to "loop" through numbers 1 to 20 and re-run the code automatically


Any tips?

Appreciated

Best reg

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Use a %do loop, but it needs to be within a macro.

%macro repeat_phreg(ntimes=);

%do var=1 %to &ntimes;

proc phreg data=have ;

strata country;

class sex ;

model time*event&var(0)=factor&var age weight / rl;

ods output parameterestimates = CorrelationEstimate&var;

run;

%end;


%mend;


%repeat_phreg(ntimes=20);


View solution in original post

8 REPLIES 8
Reeza
Super User

Use a %do loop, but it needs to be within a macro.

%macro repeat_phreg(ntimes=);

%do var=1 %to &ntimes;

proc phreg data=have ;

strata country;

class sex ;

model time*event&var(0)=factor&var age weight / rl;

ods output parameterestimates = CorrelationEstimate&var;

run;

%end;


%mend;


%repeat_phreg(ntimes=20);


ammarhm
Lapis Lazuli | Level 10

Thanks Reza, much appreciated

Rick_SAS
SAS Super FREQ

Convert the data from wide to long format. The data then has 20 BY groups with values 1-20, and a single variable (call it FACTOR) that represents the stacking of factor1-factor20.  Then run a single analysis. It will be much easier to compare the results, which are now in a single data set.

proc phreg data=have ;

by group;

strata country;

class sex ;

model time*event&var(0)=factor age weight / rl;

ods output parameterestimates = CorrelationEstimate;

run;

ammarhm
Lapis Lazuli | Level 10

Rick

A very interesting approach indeed. I even like it actually

BUT

there is a small problem: as you can see from my code, i am using the looping variable for 3 things:

%let var=1;

proc phreg data=have ;

strata country;

class sex ;

model time*event&var(0)=factor&var age weight / rl;

ods output parameterestimates = CorrelationEstimate&var;

run;

Your suggestion would solve the second and the third one, but I am also modelling for event1-20

Any suggestions on how to accommodate this in your code?

Thanks again


Reeza
Super User

The data reshape will take care of that. The last instance of &var becomes unnecessary.

data flipped;

set have;

array event(20) event1-event20;

array factor(20) factor1-factor20;

do group=1 to 20;

event_group=event(i);

factor_group=factor(i);

output;

end;

run;

proc phreg data=flipped;

by group;

strata country;

class sex ;

model time*event_group=factor_group age weight / rl;

ods output parameterestimates = CorrelationEstimate;

run;

ammarhm
Lapis Lazuli | Level 10

Correct me if I am wrong Reeza, but I think this code works if event1-20 are mutually exclusive.

In my case the same person may have event1 and event3

The same thing is with factor1-20, they are not mutually exclusive

What do you think?

Cheers

Reeza
Super User

Try it.

It doesn't matter if they're mutually exclusive or not since you have a record for each, and your code doesn't account for that in the initial program anyways.

ammarhm
Lapis Lazuli | Level 10

I am officially impressed! It did work!

Big thanks to Rick and Reeza

Cheers

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 2078 views
  • 7 likes
  • 3 in conversation