[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: Code to generate a difficult variable

From   Howard Lempel <>
To   "" <>
Subject   st: RE: Code to generate a difficult variable
Date   Thu, 5 Feb 2009 01:02:01 -0500


I can't test this because I don't currently have access to Stata, but try the following:

sort Study_id
by Study_id: gen failure = 1 if _n==_N & !missing(deathdate)
replace failure = 0 if failure==.

Hope this helps.
From: [] On Behalf Of Polis, Chelsea B. []
Sent: Wednesday, February 04, 2009 10:30 PM
Subject: st: Code to generate a difficult variable

Dear Statalisters,

I am setting up data for survival analysis on time-to-death.  I am using
time-varying covariates, so I have multiple records for each individual.
The column recording my date of death variable was imported such that the
date repeats in every record, although it will be unnecessary information
at all rounds except the last.  Different women contributed different numbers
of rounds, ranging from 1 to 8.  I'd like to generate a new variable to
indicate failure, such that failure=1 only for the last record for each
individual woman, and only if she has a recorded date of death.

To provide a visual, I have study_id, numrounds, and deathdate, available,
but would like to write code to be able to generate failure automatically.
Is this possible?  I assume it would have something to do with _n, and
perhaps a foreach command, and an if statement, but I have no idea how to
actually go about writing this, or figuring it out via help.  Any suggestions?

Study_id        numrounds       deathdate       failure
1               1               03jul2005       0
1               2               03jul2005       0
1               3               03jul2005       0
1               4               03jul2005       1
2               1               .               0
2               2               .               0
2               3               .               0
3               1               15jun2004       1
4               1               .               0

Many thanks!
Chelsea Polis

*   For searches and help try:

*   For searches and help try:

© Copyright 1996–2021 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index