BookmarkSubscribeRSS Feed

How to Survive Survival Analysis with SAS - Part II

Started ‎03-11-2016 by
Modified ‎08-03-2021 by
Views 1,868

Editor's note: SAS programming concepts in this and other Free Data Friday articles remain useful, but SAS OnDemand for Academics has replaced SAS University Edition as a free e-learning option. Hit the orange button below to start your journey with SAS OnDemand for Academics:


Access Now


A couple of weeks ago, I posted an article on Surviving Survival Analysis that I was really excited about.  I wanted to expand on that to show some of the other graph-enhancing features you could do (including changing colours), but kept running into roadblocks. After a quite of bit of head scratching, back-and-forth with SAS Technical Support, and talking to friends, I’ve finally figured out what the issue was trying to change the colours of the lines in my Kaplan-Meier curve (hint: combination new things I didn’t know Survival_analysis.jpgand me missing a minor but critical option…)


So, now that I have something to write about, let’s get down to it!




Get started with SAS OnDemand for Academics

In this 9-minute tutorial, SAS instructor @DomWeatherspoon shows you how to get your data into SAS OnDemand for Academics and other key steps:

Get Started


Get the Data

If you recall, I’m using the New York Patient Dataset, but have significantly truncated it to one site.  The 2012 data can be downloaded from here (you can also get the 2011 data). 


Getting the data readyFreeDataFriday_graphic.jpg

Before we can down to the actual analysis, we have to do some preliminary data manipulation to make our lives easier in the long run. 


We’re creating a temporary table (called work.import2) and inserting the Length_of_stay variable with a new variable “Illness_Category”, which is using the APR_Severity_of_Illness_Descript variable (the same one we used in the previous article).  The case statement takes the classifications and assigns a number in front (Minor becomes 1-Minor, etc.).  The reason we’re doing this is so that when we have the legend, the data will be sorted in a logical order. 



proc sql;

create table work.import2 as
select length_of_stay,
case when APR_Severity_of_Illness_Descript = 'Minor' then '1-Minor'
	 when APR_Severity_of_Illness_Descript = 'Moderate' then '2-Moderate'
	 when APR_Severity_of_Illness_Descript = 'Major' then '3-Major'
	 when APR_Severity_of_Illness_Descript = 'Extreme' then '4-Extreme'
end as Illness_Category
from work.import1;



The Results

The next section of code is something I’ve not introduced prior to this – but it’s relatively straightforward.  PROC TEMPLATE allows you to build a template that you want to apply to your graphs and tables. 



proc template;
   define style Styles.StatColor;
      parent = Styles.HTMLBlue;
      style GraphData1 / contrastcolor = green;
      style GraphData2 / contrastcolor = orange;
      style GraphData3 / contrastcolor = yellow;
      style GraphData4 / contrastcolor = red;



The first line is calling the style a name that is easy to remember.  The next line indicates the style you want to use as the base, in my case HTMLBlue.  The next four lines are the key ones – you have to have one “style” indicated for each category on your graph.  We have 4 categories, therefore 4 rows (Green, Orange, Yellow and Red).  The END terminates the building of the template, and then run as per usual.


The next section of code is the slightly modified PROC LIFETEST we saw in the previous article. 



ods html5(id=web) style=Styles.StatColor;

proc lifetest data=work.import2 notable plots=(survival (cb atrisk));
time length_of_stay;
strata Illness_Category;



There is in fact one difference – the ods line which specifies the template to be used. HTML5 is the output we want, and then style indicating the template we’ve created.


Here’s the output:




You can see the lines are clearly labelled, sorted in a logical order, and all with just a few lines of code.  I highly recommend reading up on PROC TEMPLATE – you can cause irreversible changes to your previously installed templates, and from what I understand they are challenging to restore.  You can do a lot with PROC TEMPLATE, from inserting images, modifying fonts, adding notes, and a wide range of other modifications.


Now it’s your turn!


Did you find something else interesting in this data? Share in the comments. I’m glad to answer any questions.





Hi there, thanks for showing how to make colorful figures. Will you please teach me how to get similar figure after adjusting for covariate. I will be very grateful if you would. Thank you for your time and effort.

Hi @Atims


Can you provide an example of what you mean?  There are a lot of great papers at - simply search for what you're looking for, and you should be able to find papers on your topic (or at least something close that you can modify).


Good luck and have a good evening


Version history
Last update:
‎08-03-2021 01:42 PM
Updated by:



Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Tags