Virata manual

stset

Declares which column is the time and which says the event happened.

What we have

  • failure(var) for any non-zero value, or failure(var==#) for one value
  • it creates _t, _d and _st, and says how many rows it had to exclude

Coming later

  • delayed entry and the other options — id(), enter(), origin(), exit() and scale() are refused rather than ignored
  • several records per subject, and time-varying covariates

You may shorten it to stse.

How you write it

stset timevar, failure(eventvar[==#])

Square brackets mean optional. A vertical bar means choose one.

What it does

Run this once before any other survival command. stset months, failure(left) says: months is how long we watched each person, and left is 1 for the people it actually happened to.

Everyone else is censored — we stopped watching before the event, so all we know is that they lasted *at least* that long. That partial information is the whole reason this family of commands exists. Dropping those people biases the answer, and counting them as "never happened" biases it the other way.

stset creates three columns behind the scenes: _t (the time), _d (did it happen) and _st (is this row usable). It reports how many rows it had to exclude, and why.

Watch out. A time of zero, negative or missing cannot be used and is excluded. Read the count it prints — if it is large, something is wrong upstream.

Try it

Every example below was run after sysuse vira.sample03, clear.

. stset tenure_months, failure(left)
     Survival-time data settings

         Failure event: left != 0 & left < .
Observed time interval: (0, tenure_months]
     Exit on or before: failure

--------------------------------------------------------------------------
    4,000  total observations
        0  exclusions
--------------------------------------------------------------------------
    4,000  observations remaining, representing
    1,652  failures in single-record/single-failure data
   87,299  total analysis time at risk and under observation
                                            At risk from t =         0
                                      Last observed exit t =        60
The same thing in pandas and in R

pandas

import numpy as np
import pandas as pd

time = pd.to_numeric(df["tenure_months"], errors="coerce")
status = pd.to_numeric(df["left"], errors="coerce")
valid = time.notna() & (time > 0)
df["_st"] = valid.astype(int)
df["_d"] = ((status.notna() & (status != 0)) & valid).astype(int)
df["_t"] = time.where(valid)
df["_t0"] = np.where(valid, 0.0, np.nan)

R

library(survival)
surv <- Surv(tenure_months, left)

# This R has NOT been run against a real R yet: read it as documentation.
The full built-in help (help stset)
stset timevar, failure(eventvar[==#])
sts list | graph | test  [, by(var) at(times)]
stcox covariates [, breslow | efron]

Survival analysis: how long until something happens, when for some subjects
it has not happened yet.  Someone still employed when the study ends has told
you they lasted at least that long -- dropping them, or counting them as
stayers, both give the wrong answer.  That is what "censored" means.

    . stset months, failure(left)          declares the time and the event
    . sts list, by(dept) at(12 24 36)      Kaplan-Meier survivor function
    . sts graph, by(dept)                  the same as a chart
    . sts test dept                        log-rank test across departments
    . stcox age i.dept                     hazard ratios

  stset      makes _st (usable), _d (event), _t (time).  failure(left) counts
             any non-zero value; failure(status==2) only that value.  A time of
             zero, negative or missing is excluded, and it says how many
  sts list   survivor function with Greenwood standard errors and a log-log
             95% interval.  With at(), one row per interval: how many were at
             risk at its start, and how many failed and were censored in it
  sts test   the log-rank test, with observed and expected events per group
  stcox      Breslow ties by default, as in Stata; add efron for Efron

Not supported, and refused rather than ignored: delayed entry, several records
per subject, time-varying covariates, stratified and parametric models,
weights, Nelson-Aalen, and tests other than log-rank.

Every number matches R's survival package (survfit, survdiff, coxph); none has
been compared with a real Stata run.

Nearby

sts stcox