Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, and its replacement, **statalist.org** is already up and running.

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

From |
Nick Cox <njcoxstata@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: elapsed time between incidents |

Date |
Tue, 19 Feb 2013 23:19:38 +0000 |

I don't think anyone can be expected to choose the same words as me, so searching for this inevitably seems difficult. Nick On Tue, Feb 19, 2013 at 9:34 PM, Steven Archambault <archstevej@gmail.com> wrote: > Okay, thanks for this. I apologize for not finding the tip prior to > posting. My chosen search words were off! > > On Tue, Feb 19, 2013 at 2:08 PM, Nick Cox <njcoxstata@gmail.com> wrote: >> This kind of problem was discussed in my posts >> >> http://www.stata.com/statalist/archive/2011-01/msg00999.html >> >> http://www.stata.com/statalist/archive/2012-08/msg00633.html >> >> and in a Tip >> >> Cox, N.J. 2011. Stata tip 101: Previous but different. Stata Journal >> 11(3): 472-473. >> >> No doubt there are other references. >> >> Here's some code: >> >> . clear >> >> . input id day incident >> >> id day incident >> 1. 1 1 6 >> 2. 1 2 . >> 3. 1 3 . >> 4. 1 4 . >> 5. 1 5 8 >> 6. 1 6 . >> 7. 1 7 5 >> 8. end >> >> . bysort id (day): gen prev = day[_n-1] if incident[_n-1] < . >> (5 missing values generated) >> >> . by id: replace prev = prev[_n-1] if missing(prev) >> (4 real changes made) >> >> . gen elapsed = day - prev if incident < . >> (5 missing values generated) >> >> . l, sep(0) >> >> +--------------------------------------+ >> | id day incident prev elapsed | >> |--------------------------------------| >> 1. | 1 1 6 . . | >> 2. | 1 2 . 1 . | >> 3. | 1 3 . 1 . | >> 4. | 1 4 . 1 . | >> 5. | 1 5 8 1 4 | >> 6. | 1 6 . 5 . | >> 7. | 1 7 5 5 2 | >> +--------------------------------------+ >> >> The nub of the matter is that the previous event can be picked up in >> the following observation for the same identifier. After that we just >> copy down as required in a cascade. >> >> FAQ . . . . . . . . . . . . . . . . . . . . . . . Replacing missing values >> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox >> 2/03 How can I replace missing values with previous or >> following nonmissing values? >> http://www.stata.com/support/faqs/data/missing.html >> >> -by:- makes doing this by panels no more difficult than doing it if >> there were no panels. >> >> Steve said non-zero, but his example implies non-missing; whatever the >> exact condition is can no doubt be substituted easily. >> >> Nick >> >> On Tue, Feb 19, 2013 at 8:45 PM, Steven Archambault >> <archstevej@gmail.com> wrote: >> >>> I have unbalanced panel data where an incident (measured by a non-zero >>> value) occurs randomly in the panel. I want to calculate the time >>> (days_elapsed) between incidents. Here is an example: >>> >>> id day incident days_elapsed >>> 1 1 6 . >>> 1 2 . . >>> 1 3 . . >>> 1 4 . . >>> 1 5 8 4 >>> 1 6 . . >>> 1 7 5 2 >>> >>> Thanks for any help here. I have not been able to find this. * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**Re: st: elapsed time between incidents***From:*Nick Cox <njcoxstata@gmail.com>

**References**:**st: elapsed time between incidents***From:*Steven Archambault <archstevej@gmail.com>

**Re: st: elapsed time between incidents***From:*Nick Cox <njcoxstata@gmail.com>

**Re: st: elapsed time between incidents***From:*Steven Archambault <archstevej@gmail.com>

- Prev by Date:
**st: solved** - Next by Date:
**RE: st: Quantile Regression with FE** - Previous by thread:
**Re: st: elapsed time between incidents** - Next by thread:
**Re: st: elapsed time between incidents** - Index(es):