Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: elapsed time between incidents


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: elapsed time between incidents
Date   Tue, 19 Feb 2013 21:08:14 +0000

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
<[email protected]> 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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index