Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: elapsed time between incidents


From   David Kantor <kantor.d@att.net>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: elapsed time between incidents
Date   Tue, 19 Feb 2013 16:10:16 -0500

At 03:45 PM 2/19/2013, Steve wrote:
Hi all,

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.

-Steve

First, you mentioned an incident being determined by a non-zero value, presumably in the variable name incident. But your example seems to be based on non-missing, rather than non-zero. I'll presume you mean non-missing.

You could try...
gen pday = day if ~mi(incident)
by id (day): replace pday = pday[_n-1] if mi(pday) & _N>1
/* you could also use -carryforward- from ssc. */

by id (day): gen days_elapsed = day- pday[_n-1] if  ~mi(incident) & _n>1

HTH
--David

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index