DATA Step, Macro, Functions and more

Use Previous Observation Values to Calculate Current Observation

Accepted Solution Solved
Reply
Regular Contributor
Posts: 237
Accepted Solution

Use Previous Observation Values to Calculate Current Observation

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
............

Accepted Solutions
Solution
‎10-28-2011 09:20 AM
PROC Star
Posts: 7,474

Re: Use Previous Observation Values to Calculate Current Observation

Posted in reply to djbateman

%let AtRisk=49;

data want (drop=LastSmiley Happy;

  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


All Replies
Frequent Contributor
Posts: 104

Re: Use Previous Observation Values to Calculate Current Observation

Posted in reply to djbateman

How about this?

data data2;

     set data1;

     retain atrisk;

     if _n_ = 1 then atrisk = &atrisk;

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

run;

Solution
‎10-28-2011 09:20 AM
PROC Star
Posts: 7,474

Re: Use Previous Observation Values to Calculate Current Observation

Posted in reply to djbateman

%let AtRisk=49;

data want (drop=LastSmiley Happy;

  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

;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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