Hi, this is my first time attempting to run a KM curve in SAS. I apologize in advance if my questions are not well structured. We need to produce a KM curve for cumulative incidence from age at which subjects started treatment(Age_trt) to when they presented their first respiratory emergency (Event_age). This needs to be stratified by group1 and group2.
This is the raw dataset:
data have;
input ID Lastage group1 group2 Age_trt Event_age;
datalines;
1 12 0 1 2 4
2 13 0 1 4 6
3 11 1 0 3 7
4 12 1 0 2 .
5 14 0 1 2 .
6 12 1 0 7 9
7 11 1 0 1 6
8 14 0 1 3 .
9 13 1 0 3 .
10 13 0 1 5 8
;
run;
From what I gathered a "censored" variable is needed to run proc lifetest.
data for_test;
input ID Lastage group1 group2 Age_trt Event_age Censoring_status;
datalines;
1 12 0 1 4 1
2 13 0 1 6 1
3 11 1 0 7 1
4 12 1 0 . 0
5 14 0 1 . 0
6 12 1 0 9 1
7 11 1 0 6 1
8 14 0 1 . 0
9 13 1 0 . 0
10 13 0 1 8 1
;
run;
I am not sure how the group variable should be transformed and if so how. I am not sure how the code will need to be adapted to show the cumulative incidence instead of the more common survival analysis?
Rough idea to get you started.
data have;
input ID Lastage group1 group2 Age_trt Event_age;
datalines;
1 12 0 1 2 4
2 13 0 1 4 6
3 11 1 0 3 7
4 12 1 0 2 .
5 14 0 1 2 .
6 12 1 0 7 9
7 11 1 0 1 6
8 14 0 1 3 .
9 13 1 0 3 .
10 13 0 1 5 8
;
run;
data sample;
set have;
time2event = coalesce(event_age, lastage) - age_trt;
censor = missing(event_age);
run;
proc format;
value group_label
0 = 'Group 2'
1 = 'Group 1';
run;
proc lifetest data=sample plots=survival(f);
strata group1;
format group1 group_label;
time time2event*censor(1);
run;
Rough idea to get you started.
data have;
input ID Lastage group1 group2 Age_trt Event_age;
datalines;
1 12 0 1 2 4
2 13 0 1 4 6
3 11 1 0 3 7
4 12 1 0 2 .
5 14 0 1 2 .
6 12 1 0 7 9
7 11 1 0 1 6
8 14 0 1 3 .
9 13 1 0 3 .
10 13 0 1 5 8
;
run;
data sample;
set have;
time2event = coalesce(event_age, lastage) - age_trt;
censor = missing(event_age);
run;
proc format;
value group_label
0 = 'Group 2'
1 = 'Group 1';
run;
proc lifetest data=sample plots=survival(f);
strata group1;
format group1 group_label;
time time2event*censor(1);
run;
Thank you! For the group variables, basically we need only a variable called group to be able to stratify? And Lastage variable is needed to limit what? Sorry, I am having trouble interpreting the the graph and the use of the variable Lastage. Could you please show the figure you got and kindly explain the interpretation? It means appearance of the first event? Thank you in advance.
This is the figure:
First step is to create the time variable - specifically the time to event. Which I'm assuming is measured as the event_age - age_start treatment. However, for censored data you do not have an event, so to calculate time to event you use the last time observed, which I'm assuming is the lastAge.
Great! Thanks for explaining this. Just to confirm, for my knowledge, it is best to have a variable called group so this can be used to stratified right?
Ok, how will the proc lifetest code change if you have a group variable and you want to stratify by this variable? Let's say the values are 1 and 2 for the group variable.
Is this correct:
data sample;
set have;
time2event = coalesce(event_age, lastage) - age_trt;
censor = missing(event_age);
if group1=1 then group=1,
else group=2;
run;
proc lifetest data=sample plots=survival(f);
strata group;
time time2event*censor(1);
run;
Sorry, missed a ";"
data sample;
set have;
time2event = coalesce(event_age, lastage) - age_trt;
censor = missing(event_age);
if group1=1 then group=1;
else group=2;
run;
proc lifetest data=sample plots=survival(f);
strata group;
time time2event*censor(1);
run;
I have another question, if we were to create a more elaborate figure using proc sgplot, we need a dataset from proc lifetest, correct?
I tried using this but it didn't work:
ods output survivalplot=sp;
proc lifetest data=sample plots=survival(f);
strata group;
time time2event*censor(1);
run;
ods output close;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
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.
Ready to level-up your skills? Choose your own adventure.