Svend's technique is nice, but doesn't
produce the right answer if the event
never occurs. In that case -time- is
initialised to 7, and never changed.
A fix -- which by a interesting programming law
also yields shorter code -- is to initialise
-time- as missing.
clear
input id day1 day2 day3 day4 day5 day6 day7
1 0 0 0 1 0 0 0
2 0 0 0 0 1 1 .
3 1 1 1 1 1 1 1
4 0 0 0 0 0 0 0
5 0 0 . . . . .
end
gen time= .
gen event=0
forvalues I = 7(-1)1 {
replace time = `I' if day`I'==1
replace event = 1 if day`I'==1
}
list
The bigger message remains the advice to -reshape-.
This data structure will always be awkward for
most questions.
Nick
[email protected]
Svend Juul
> Brad wrote:
>
> I have data like the following:
> id day1 day2 day3 day4 day5 day6 ... day90
> 1 0 0 0 1 0 0 ... 0
> 2 0 0 0 0 1 1 ... .
> 3 1 1 1 1 1 1 ... 1
> 4 0 0 0 0 0 0 ... 0
>
> Assume that a 1 on day`i' represents failure. I'm trying to
> generate a
> variable that gives days to failure for survival analysis.
> E.g., subject 1
> failed on day 4, subject 5 failed on day 5, subject 3 failed on day 1,
> subject 4 did not fail and would be considered censored. 90
> days worth of
> data were collected but not all subjects were observed for
> the full 90-day
> period. Also note that subjects may be recorded as failing
> (drinking at
> hazardous levels) on any combination of days during the
> 90-day period. I'm
> trying to calculate number of days to first failure....
> ---------------------------------------------------------------------
>
> In the following example I use only 7 days, but the principle
> is the same. To find the first day of changed status, examine
> days in reverse order:
>
> clear
> input id day1 day2 day3 day4 day5 day6 day7
> 1 0 0 0 1 0 0 0
> 2 0 0 0 0 1 1 .
> 3 1 1 1 1 1 1 1
> 4 0 0 0 0 0 0 0
> 5 0 0 . . . . .
> end
>
> gen time=7
> gen event=0
> forvalues I = 7(-1)1 {
> replace time = `I' if day`I'==1
> replace event = 1 if day`I'==1
> replace time = `I'-1 if day`I'>=.
> }
> list
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/