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
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);
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);
Thanks Reza, much appreciated
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;
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
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;
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
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.
I am officially impressed! It did work!
Big thanks to Rick and Reeza
Cheers
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.