Hello, I am trying to compute S using the data with variable N and time. See example below. My code below does not work:
N time S
1 2 2 S[1]=time
1 5 5-2 = time[i+1]-time
1 7 7-5 = time[i+1]-time
2 5 5 S[1]=time
2 7 7-5 = time[i+1]-time
2 8 8-7 = time[i+1]-time
2 10 10-8 = time[i+1]-time
3 1 1
3 7 7-1
3 9 9-7
3 9 9-9
proc iml;
use simulate1_&u;
read all var{N time } into DM;
close;
time = DM[,2]; N = DM[,1];
n = nrow(DM);
TT=J(nrow(time),1,1);
do i = 1 to n;
if i=1 then S=time;
else if 1 < i < =n then S=time[i+1]-time;
end;
create kaplann_&u var{time N S };
append;
quit;
There are several ways to do this. If the N variable is unsorted, you could use the UNIQUE-LOC technique. I assume the data are sorted by N, in which case it is more efficient to use the UNIQUEBY technique.
There is no need to loop over the observations to compute time-time[i-1]. Instead you can use the DIF function.
data a;
input N time @@;
datalines;
1 2 1 5 1 7 2 5 2 7 2 8 2 10 3 1 3 7 3 9 3 9
;
proc iml;
use A;
read all var{N time}; /* assume data sorted by N */
close;
nobs = nrow(time);
S = J(nobs,1,.);
b = uniqueby(N, 1); /* b = beginning of i_th category */
b = b // (nrow(N)+1); /* trick: append (n+1) to end of b */
do i = 1 to nrow(b)-1;
idx = b:(b[i+1]-1); /* Find observations in i_th level */
S[idx] = dif(time[idx]); /* DIF of time steps */
S[idx[1]] = time[idx[1]]; /* replace missing with 1st time */
end;
print S;
My data is simulated data, so it not fixed. That was just an example. I think I can fix your code to work with my simulation.
If you read the articles, they describe how to handle other cases, such as unsorted data.
I tried to modify your code to to both sum and diffrence but it does not work:
proc iml;
use simulate1_&u;
read all var{TIME OLF SURVIVAL1} into DM;
close;
SURVIVAL1 = DM[,3]; OLF = DM[,2]; TIME = DM[,1];
n = nrow(DM);
segment = J(nrow(DM),1,1); *print segment;
SS=J(nrow(time),1,1);
nobs = nrow(time);
Survival1 = J(nobs,1,.);
a = uniqueby(time , 1); /* a = beginning of i_th category */
a = a // (nrow(time )+1); /* trick: append (n+1) to end of a */
do i = 1 to nrow(a)-1;
idx = a:(a[i+1]-1); /* Find observations in i_th level */
SS[idx] = sum(survival1[idx]); /* sum of time steps */
SS[idx[1]] = survival1[idx[1]]; /* replace missing with 1st survival1 */
TT=J(nrow(survival1),1,1);
nobs = nrow(survival1);
Survival1 = J(nobs,1,.);
b = uniqueby(Survival1 , 1); /* b = beginning of i_th category */
b = b // (nrow(Survival1 )+1); /* trick: append (n+1) to end of b */
do i = 1 to nrow(b)-1;
idx = b:(b[i+1]-1); /* Find observations in i_th level */
TT[idx] = dif(time[idx]); /* DIF of time steps */
TT[idx[1]] = time[idx[1]]; /* replace missing with 1st time */
end;
create kaplann_&u var{time OLF survival1 SS TT };
append;
quit;
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 16. Read more here about why you should contribute and what is in it for you!
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.