BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
djbateman
Lapis Lazuli | Level 10

I know that SAS can do this with PROC LIFETEST, but I want to attempt this by hand.  Even though I can easily do this in Excel or R, this might be a good learning experience.  I am essentially trying to reproduce the product-limit table for some survival data.  Below is a look at how my data currently looks.  The value in "AtRisk" is a macro variable placed in the dataset if it is the first observation.

What I want to do is calculate AtRisk(i) = AtRisk(i-1)-censored(i-1)-event(i-1).  In other words, AtRisk for SurvivalTime=2 is 49-5-0=44.  Then At Risk for SurvivalTime=2 is 44-0-1=43, etc.

I have tried using the RETAIN statement and the lag() function to perform this, but I cannot get either one to work.  If you have any better ideas, please help me out!

Here is an example of my code that does not do the trick:

data data2;

     set data1;

     by _LABEL_;     /* NOTE: _LABEL_ has the same value throughout the data */

     if first._LABEL_ then atrisk=&atrisk.;

           else atrisk=lag(atrisk);

run;

CURRENT DATA:

_NAME_SurvivalTimeCensorredEventAtRisk
COUNT15049
COUNT201.
COUNT301.
COUNT410.
COUNT501.
...............

CORRECT DATA:

SurvivalTimeCensoredEventAtRisk
15049
20144
30143
41042
50141
............
1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

%let AtRisk=49;

data want (drop=Last:);

  input _NAME_ $ SurvivalTime Censorred Event;

  retain AtRisk LastAtRisk;

  AtRisk=ifn(_n_ eq 1,&AtRisk,

    LastAtRisk);

  LastAtRisk=AtRisk-Censorred-Event;

  cards;

COUNT          1          5          0

COUNT          2          0          1

COUNT          3          0          1

COUNT          4          1          0

COUNT          5          0          1

;

View solution in original post

2 REPLIES 2
DLing
Obsidian | Level 7

How about this?

data data2;

     set data1;

     retain atrisk;

     if _n_ = 1 then atrisk = &atrisk;

     else atrisk = atrisk - censored - lag( event );    

run;

art297
Opal | Level 21

%let AtRisk=49;

data want (drop=Last:);

  input _NAME_ $ SurvivalTime Censorred Event;

  retain AtRisk LastAtRisk;

  AtRisk=ifn(_n_ eq 1,&AtRisk,

    LastAtRisk);

  LastAtRisk=AtRisk-Censorred-Event;

  cards;

COUNT          1          5          0

COUNT          2          0          1

COUNT          3          0          1

COUNT          4          1          0

COUNT          5          0          1

;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1529 views
  • 0 likes
  • 3 in conversation