proc lifetest data=sascomp.adqs method=PL plots=(survival(atrisk cl) logsurv) alpha=0.05;
time RETIME*STATUS(0);
strata Group;
run;
This is my PROC LIFETEST program, which can output a survival plot for me.
I've already know if I add survival(test), then I can add log rank's P-value on my plot, which is:
proc lifetest data=sascomp.adqs method=PL plots=(survival(test atrisk cl) logsurv) alpha=0.05;
time RETIME*STATUS(0);
strata Group;
run;
But I don't know how to add median survival time and HR from Cox. Please help me, many thanks!
Hello @morenayan,
I see two possible approaches: One was suggested in the 2023 post Re: Kaplan Meier using proc lifetest and involves reproducing the Kaplan-Meier plot with PROC SGPLOT and using TEXT statements. The second approach might be simpler because it just modifies the existing Kaplan-Meier plot (template), which is relatively easy thanks to the comprehensive macros that SAS has provided for this purpose (see Controlling the Survival Plot by Modifying Graph Templates). Below I provide an example using the SAS-supplied dataset SASHELP.BMT, based on the example Adding a Small Inset Table with Event Information from the documentation.
/* Create sample data for demonstration */
data have;
set sashelp.bmt(where=(group ne 'ALL'));
run;
/* Compute statistics */
ods select none;
ods output homtests=ht quartiles=qt;
proc lifetest data=have;
time t*status(0);
strata group;
run;
ods output parameterestimates=est;
proc phreg data=have;
class group;
model t*status(0)=group / rl;
run;
ods select all;
/* Combine and format statistics */
data stats;
merge qt(where=(percent=50) drop=t:)
est(keep=ClassVal0 h: rename=(ClassVal0=Group));
by group;
length hr $20;
if hazardratio=. then hr='Ref';
else hr=put(HazardRatio,best4.)||' ('||put(HRLowerCL,best4.)||'-'||put(HRUpperCL,best4.)||')';
run;
proc format;
value med
.='NE'
other=[4.];
run;
/* Write statistics to macro variables */
proc sql noprint;
select probchisq into :pval from ht where test like 'L%';
select group, estimate format=med., lowerlimit format=med., upperlimit format=med., hr
into :grp1-, :med1-, :lcl1-, :ucl1-, :hr1- from stats;
quit;
/* Modify inset of Kaplan-Meier plot to contain statistics */
%ProvideSurvivalMacros /* from https://support.sas.com/documentation/onlinedoc/stat/ex_code/151/templft.html */
%let InsetOpts = ;
%let LegendOpts = title="+ Censored" location=inside autoalign=(Bottom);
%macro StmtsBottom;
%let t = / textattrs=(weight=bold);
layout gridded / columns=3 border=TRUE autoalign=(TopRight);
entry halign=left "Logrank p=&pval"; entry " "; entry " ";
entry " "; entry " "; entry " ";
entry halign=left "Group" &t; entry "Median (95% CI)" &t; entry "HR (95% CI)" &t;
entry halign=left "&grp1"; entry "&med1 (&lcl1-&ucl1)"; entry "&hr1";
entry halign=left "&grp2"; entry "&med2 (&lcl2-&ucl2)"; entry "&hr2";
endlayout;
%mend;
%CompileSurvivalTemplates
/* Create the modified Kaplan-Meier plot */
ods graphics on;
proc lifetest data=have plots=(survival(test atrisk cl) logsurv);
time t*status(0);
strata group;
run;
Result:
Hello @morenayan,
I see two possible approaches: One was suggested in the 2023 post Re: Kaplan Meier using proc lifetest and involves reproducing the Kaplan-Meier plot with PROC SGPLOT and using TEXT statements. The second approach might be simpler because it just modifies the existing Kaplan-Meier plot (template), which is relatively easy thanks to the comprehensive macros that SAS has provided for this purpose (see Controlling the Survival Plot by Modifying Graph Templates). Below I provide an example using the SAS-supplied dataset SASHELP.BMT, based on the example Adding a Small Inset Table with Event Information from the documentation.
/* Create sample data for demonstration */
data have;
set sashelp.bmt(where=(group ne 'ALL'));
run;
/* Compute statistics */
ods select none;
ods output homtests=ht quartiles=qt;
proc lifetest data=have;
time t*status(0);
strata group;
run;
ods output parameterestimates=est;
proc phreg data=have;
class group;
model t*status(0)=group / rl;
run;
ods select all;
/* Combine and format statistics */
data stats;
merge qt(where=(percent=50) drop=t:)
est(keep=ClassVal0 h: rename=(ClassVal0=Group));
by group;
length hr $20;
if hazardratio=. then hr='Ref';
else hr=put(HazardRatio,best4.)||' ('||put(HRLowerCL,best4.)||'-'||put(HRUpperCL,best4.)||')';
run;
proc format;
value med
.='NE'
other=[4.];
run;
/* Write statistics to macro variables */
proc sql noprint;
select probchisq into :pval from ht where test like 'L%';
select group, estimate format=med., lowerlimit format=med., upperlimit format=med., hr
into :grp1-, :med1-, :lcl1-, :ucl1-, :hr1- from stats;
quit;
/* Modify inset of Kaplan-Meier plot to contain statistics */
%ProvideSurvivalMacros /* from https://support.sas.com/documentation/onlinedoc/stat/ex_code/151/templft.html */
%let InsetOpts = ;
%let LegendOpts = title="+ Censored" location=inside autoalign=(Bottom);
%macro StmtsBottom;
%let t = / textattrs=(weight=bold);
layout gridded / columns=3 border=TRUE autoalign=(TopRight);
entry halign=left "Logrank p=&pval"; entry " "; entry " ";
entry " "; entry " "; entry " ";
entry halign=left "Group" &t; entry "Median (95% CI)" &t; entry "HR (95% CI)" &t;
entry halign=left "&grp1"; entry "&med1 (&lcl1-&ucl1)"; entry "&hr1";
entry halign=left "&grp2"; entry "&med2 (&lcl2-&ucl2)"; entry "&hr2";
endlayout;
%mend;
%CompileSurvivalTemplates
/* Create the modified Kaplan-Meier plot */
ods graphics on;
proc lifetest data=have plots=(survival(test atrisk cl) logsurv);
time t*status(0);
strata group;
run;
Result:
HI, thanks!!!
i've already solved my problem perfectly by using the second solutions you've provived.
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.