Obsidian | Level 7

## How to calculate survival time?

Hi SAS community,

I hope you are doing well. When conducting survival analysis, I used two ways to define the endpoint of the study period:

1. Individuals who are diagnosed with depression during the study period can be defined as the endpoint.
2. Alternatively, individuals who do not have depression at the end of the study period can be defined as the endpoint.
I was wondering how I could calculate my survival time.

This is original data:

 idauniq depression wave 100052 0 3 100052 0 4 100052 0 5 100052 0 6 100052 0 7 100052 0 8 100052 0 9 100055 0 3 100055 0 4 100055 0 5 100055 0 6 100055 0 7 100055 0 8 100057 0 3 100057 0 4 100057 0 5 100057 0 6 100057 0 7 100057 0 8 100057 0 9 100059 0 3 100059 0 4 100059 1 5 100061 0 3 100061 1 4 100061 0 5 100061 0 6 100068 0 3 100068 0 5 100068 0 6 100068 1 7 100080 0 3 100080 0 5 100081 0 3 100081 0 4 100081 0 5 100081 0 6 100081 1 7

This is results what is expect:

 idauniq depression wave Time censor 100052 0 9 12 0 100055 0 8 10 0 100057 0 9 12 0 100059 1 5 4 1 100061 0 4 2 0 100068 1 7 8 1 100080 0 5 4 0 100081 1 7 8 1

1 ACCEPTED SOLUTION

Accepted Solutions
PROC Star

## Re: How to calculate survival time?

You can follow a SET statement with

``  by idauniq ;``

which allows you to determine if the observation-in-hand is the first (or last) obs for a given idauniq.

You will output one obs per idauniq.  It will be either the last obs (if there are no preceding depression obs) or else the first obs with depressio=1:

``````data have;
input idauniq	depression	wave;
datalines;
100052	0	3
100052	0	4
100052	0	5
100052	0	6
100052	0	7
100052	0	8
100052	0	9
100055	0	3
100055	0	4
100055	0	5
100055	0	6
100055	0	7
100055	0	8
100057	0	3
100057	0	4
100057	0	5
100057	0	6
100057	0	7
100057	0	8
100057	0	9
100059	0	3
100059	0	4
100059	1	5
100061	0	3
100061	1	4
100061	0	5
100061	0	6
100068	0	3
100068	0	5
100068	0	6
100068	1	7
100080	0	3
100080	0	5
100081	0	3
100081	0	4
100081	0	5
100081	0	6
100081	1	7
run;
data want (drop=n_dep);
set have ;
by idauniq ;
if first.idauniq then n_dep=0;
n_dep+depression;
if (n_dep=1 and depression=1) or (n_dep=0 and last.idauniq=1);
time=2*(wave-3);
run;
``````

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
3 REPLIES 3
PROC Star

## Re: How to calculate survival time?

And what is the rule by which you calculate the TIME variable?

 idauniq depression wave 100052 0 3 100052 0 4 100052 0 5 100052 0 6 100052 0 7 100052 0 8 100052 0 9

From that you get

 idauniq depression wave Time censor 100052 0 9 12 0

How did you get time=12?

Also, if an individual has depression=1 in a given wave, does that mean you will ignore subsequent waves for that individual?  For instance, see

 idauniq depression wave
 100061 0 3 100061 1 4 100061 0 5 100061 0 6
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Obsidian | Level 7

## Re: How to calculate survival time?

I am sorry that I did not mention that this study was conducted every two years. I started using wave 3. The time between wave3 to wave 4 is two years. (Wave9- wave3) *2=12

Yes, if an individual has depression=1 in a given wave, I will ignore subsequent waves for that individual.

Thanks!
PROC Star

## Re: How to calculate survival time?

You can follow a SET statement with

``  by idauniq ;``

which allows you to determine if the observation-in-hand is the first (or last) obs for a given idauniq.

You will output one obs per idauniq.  It will be either the last obs (if there are no preceding depression obs) or else the first obs with depressio=1:

``````data have;
input idauniq	depression	wave;
datalines;
100052	0	3
100052	0	4
100052	0	5
100052	0	6
100052	0	7
100052	0	8
100052	0	9
100055	0	3
100055	0	4
100055	0	5
100055	0	6
100055	0	7
100055	0	8
100057	0	3
100057	0	4
100057	0	5
100057	0	6
100057	0	7
100057	0	8
100057	0	9
100059	0	3
100059	0	4
100059	1	5
100061	0	3
100061	1	4
100061	0	5
100061	0	6
100068	0	3
100068	0	5
100068	0	6
100068	1	7
100080	0	3
100080	0	5
100081	0	3
100081	0	4
100081	0	5
100081	0	6
100081	1	7
run;
data want (drop=n_dep);
set have ;
by idauniq ;
if first.idauniq then n_dep=0;
n_dep+depression;
if (n_dep=1 and depression=1) or (n_dep=0 and last.idauniq=1);
time=2*(wave-3);
run;
``````

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Discussion stats
• 3 replies
• 368 views
• 0 likes
• 2 in conversation