I am learning SGPLOT to output the KM plots recently, I have some questions how to customize the Kaplan plot output. I tried to achieve it using the sas documentation, but I am not able to achieve it. I have the questions mentioned in the image. Can you guys please help me to learn the things, also please direct any good references. Thank you in Advance
**Dummy data**;
data adtte;
length paramcd $8 param $100;
paramcd = "OS";
param = "Overall Survival";
do trtpn = 1 to 4;
trtp = 'Treatment ' || put(trtpn,1.);
do subjid = 1 to 40;
aval = 2 + ranuni(1)*24*(trtpn/4);
cnsr = (ranuni(1)>0.2);
output;
end;
end;
run;
proc sort data=adtte out=km00;
by trtpn trtp;
where paramcd = "OS";
run;
proc sql noprint;
select distinct strip(param) || " Probability"
into :ylabel
from km00
;
quit;
%let ylabel = &ylabel;
%put &=ylabel;
* creating tick marks on X-axis*;
%let values = 0 2 4 6 8 10 12 14 16 18 20 22 24;
%put &=values;
**format the value 0 as B/L**;
proc format;
value timefmt
0 = "B/L"
other = [best.]
;
run;
ods html;
**lifetest**;
ods graphics on;
ods listing close;
proc lifetest data=km00 plots=(survival(atrisk=&values));
ods output survivalplot=km10;
time aval*cnsr(1);
strata trtpn;
run;
**complete with a format to prevent missing values from showing up as a dot.**;
proc format;
value xatn
. = " "
other = [best.]
;
run;
data km20;
set km10;
if n(tatrisk) then
xatn = atrisk;
format xatn xatn.;
run;
****************************************************************************
Next we add the xaxistable statement. For some reason SAS has us use the
class= option to create groups in the x-axis table (we use the group= option
in the step and scatter statements).
*****************************************************************************;
proc sgplot data=km20;
*--- draw the survival curves ---;
step y=survival x=time /
group=stratumnum
name="step"
;
*--- draw censor indicators for censoring symbol legend purposes ---;
scatter y=censored x=time /
markerattrs=(symbol=circle )
name="censor"
;
*--- draw censor indicators ---;
scatter y=censored x=time /
group=stratumnum
markerattrs=(symbol=circle)
;
*--- add the sample sizes ---;
xaxistable xatn /
class=stratumnum
colorgroup=stratumnum
;
*--- cosmetics ---;
yaxis
label="&ylabel"
;
xaxis
values=(&values)
valueshint
valuesformat=timefmt.
label="Weeks" ;
**--Key legend--> Main heading--**;
keylegend "step" /
title="Treatment Group"
noborder
linelength=15pct
outerpad=(bottom=10pt)
;
**---Location of the Censor legend indicator--**;
keylegend "censor" /
location=inside
position=bottomleft
;
run;
Check out the very popular SAS macro(s) created by @JeffMeyers :
Kaplan-Meier Survival Plotting Macro %NEWSURV
A SAS Macro to Perform Kaplan-Meier Multiple Imputation for Survival Analyses with Competing Events
I am new KM plot, where I can find %newsurv macro code?
All of the SAS programs are attached to the article. I believe the most recent version is this one.
This is also a popular paper :
Paper 427-2013
Creating and Customizing the Kaplan-Meier Survival Plot in PROC LIFETEST
Warren F. Kuhfeld and Ying So, SAS Institute Inc.
https://support.sas.com/resources/papers/proceedings13/427-2013.pdf
From the same author :
Creating and customizing the Kaplan-Meier Survival Plot in PROC LIFETEST
Best regards,
Koen
Thanks for references to my papers, but it is far better to check with the SAS documentation.
https://documentation.sas.com/doc/en/pgmsascdc/v_018/statug/statug_kaplan_toc.htm
For more general customizations, see:
https://documentation.sas.com/doc/en/pgmsascdc/v_018/statug/statug_odsgraph_toc.htm
https://documentation.sas.com/doc/en/pgmsascdc/v_018/statug/statug_templt_toc.htm
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.