I'm doing a survival analysis and wanted to know if there's a better way to write the procedure below with an array and a loop.
I want to be able to specific a number of weeks in that last bit of code without having to write it all out. I'm trying to get the cumulative sum starting from variable week position (week is the fail week) going back n number of weeks. The variables w1-w52 are the status of a piece of equipment so I'm looking to see if a piece of equipment has been running for consecutive weeks prior to fail.
proc phreg data=dataset;
model week*failure(1) = var1 var2 var3 var4 week week_runtime/ ties = efron;
array weeks(*) w1-w52;
/* This is the status of equipment at the week of fail */
equipment_sts = weeks[week];
/* This is where I'm trying to make my code more efficient so I can specify n weeks */
if sum(weeks[week] + weeks[week-1] + weeks[week-2] + weeks[week-3] .... weeks[week - n]) = n then week_runtime = 1;
else week_runtime = 0;
run;
It is not clear what is the logic you are trying to create. Sounds like you want a boolean flag, WEEK_RUNTIME, that is true when failure has not happened by WEEK.
I don't know if programming inside of PROC PHREG supports the full features of the DATA step but in a data step you could just use an iterative DO loop with a WHILE() clause to stop once you have found a failure.
data have ;
input week week1-week5 ;
cards;
2 1 1 1 0 0
3 1 1 0 0 0
4 1 1 1 1 1
;
data want;
set have;
array weeks week1-week5 ;
week_runtime=1;
do index=1 to min(week,dim(weeks)) while (week_runtime=1);
week_runtime=weeks[index];
end;
drop index;
run;
Results:
week_
Obs week week1 week2 week3 week4 week5 runtime
1 2 1 1 1 0 0 1
2 3 1 1 0 0 0 0
3 4 1 1 1 1 1 1
PS If you do put this code into PROC PHREG watch out as I don't think that the original simpler ARRAY syntax is supported in procedures. You will have to add the superfluous [*]
token between the name of the array and the list of variables.