Stata Textbook Examples
Applied Survival Analysis

Supplementary notes
How to create categorical time dependent variables in STATA

Please Note: Stata graph commands changed with version 8 and this page was developed before version 8 was released and uses Stata 7 graph commands.  Please see How do I use version 7 graph commands in Stata version 8? for information on how to either run these Stata 7 graph commands in Stata version 8, or how you can covert these commands to use Stata 8 syntax.

Let's first discussed the usage of categorical time-dependent variables.  These variables are extremely useful for multi-state models. An example of a multi-stage could be when subjects are in or out of a hospital or other treatment programs. Thus, we would like to have a variable which would be equal to one when the subject is in the hospital and is equal to zero when the subject has been discharged from the hospital.

To illustrate how to generate a categorical, in this instance a dichotomous, time-dependent variable we will use the model shown in table 7.3 on p. 252 in the book "Applied Survival Analysis" by Hosmer and Lemeshow.  This model includes a time-dependent variable called off_trt. The data set that we'll be using is the UIS data set and a description of the data can be found on p. 23 of the book. The main focus of the model is to predict the length of time until a subject starts using drugs again.  The main variables that we will consider are time, los and censor.  The variable time is the total length of time from admission until the subject was no longer in the study either because the subject returned to drug use or the subject was lost to follow up (censored).  The variable los measures the length of time from admission until the subject left the treatment program. The variable censor indicates whether the subject returned to drug use (censor=1) or was lost to follow up (censor=0).

Suppose we wanted a variable called off_trt that would be an indicator that the subject has left the treatment program.  The variable off_trt would equal zero if time <= los and equal 1 if time > los.

use uis, clear
gen id = _n

   

It is actually really straightforward in Stata to create such a variable. The command stsplit is all we need here.

stset time, f(censor) id(ID)
stsplit off_trt, at(0) after(_t=los) 
replace off_trt = off_trt + 1
list id los time _t _t0 _d off_trt in 45/55, clean nol 

       id   los   time    _t   _t0   _d   off_trt  
 45.   24    48     48    48     0    0         0  
 46.   24    48     53    53    48    1         1  
 47.   25    90     90    90     0    0         0  
 48.   25    90    225   225    90    1         1  
 49.   26    91     91    91     0    0         0  
 50.   26    91    161   161    91    1         1  
 51.   27    87     87    87     0    1         0  
 52.   28    88     88    88     0    0         0  
 53.   28    88     89    89    88    1         1  
 54.   29     9      9     9     0    0         0  
 55.   29     9     44    44     9    1         1  
Then we create the variables need in the model in table 5.11, p. 252.
gen ivhx3 = ivhx==3 
gen agsi = age*site 
gen rasi = race*site 
fracgen ndrugtx -1 -1 
rename ndrugt_1 fp1 
rename ndrugt_2 fp2  
stcox age becktota fp1 fp2 ivhx3 race treat site agsi rasi off_trt, nohr 
No. of subjects =          575                     Number of obs   =      1079
No. of failures =          464
Time at risk    =       138900
                                                   LR chi2(11)     =    404.66
Log likelihood  =   -2461.6565                     Prob > chi2     =    0.0000

------------------------------------------------------------------------------
          _t |
          _d |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         age |   -.037877   .0100552    -3.77   0.000    -.0575849   -.0181691
    becktota |   .0079745   .0049148     1.62   0.105    -.0016584    .0176073
         fp1 |  -.6086314   .1283462    -4.74   0.000    -.8601853   -.3570775
         fp2 |  -.2255842   .0495962    -4.55   0.000     -.322791   -.1283774
       ivhx3 |   .2746718   .1089458     2.52   0.012     .0611419    .4882016
        race |  -.5169569   .1344993    -3.84   0.000    -.7805707   -.2533432
       treat |   .0193972   .0961311     0.20   0.840    -.1690164    .2078108
        site |  -.9690888   .5158775    -1.88   0.060     -1.98019    .0420125
        agsi |   .0363502   .0158037     2.30   0.021     .0053755    .0673249
        rasi |   .5109338    .256902     1.99   0.047     .0074152    1.014452
     off_trt |   2.571152   .1567619    16.40   0.000     2.263904      2.8784
------------------------------------------------------------------------------

How to cite this page

Report an error on this page or leave a comment

The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.