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

# Re: st: Capturing the date and which something first occurs

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: Capturing the date and which something first occurs Date Fri, 6 Apr 2012 18:41:17 +0100

```When the first occurrence occurred is discussed in the same FAQ

http://www.stata.com/support/faqs/data/firstoccur.html

Here is another way to do it.

egen date_first = min(time / state), by(id)

Explanation: This is the trick that dividing by zero can be useful.
time / 0 is returned as missing and thus ignored in the calculation of
the minimum, as long as the -state- did occur.

Nick

On Fri, Apr 6, 2012 at 6:28 PM, Richard T. Campbell <dcamp@uic.edu> wrote:

> Suppose I have a data set like that used by Nick Cox in an FAQ which shows
> how to capture a
> record at which something first occurs. Here is his example.
>
>
>     +---------------------------+
>     | id   time   state   first |
>     |---------------------------|
>  1. |  1      1       0       0 |
>  2. |  1      2       0       0 |
>  3. |  1      3       0       0 |
>  4. |  1      4       1       1 |
>  5. |  1      5       1       0 |
>  6. |  1      6       1       0 |
>  7. |  1      7       1       0 |
>  8. |  1      8       1       0 |
>  9. |  1      9       1       0 |
>  10. |  1     10       1       0 |
>     |---------------------------|
>  11. |  2      2       0       0 |
>  12. |  2      2       0       0 |
>  13. |  2      3       1       1 |
>  14. |  2      4       1       0 |
>  15. |  2      5       1       0 |
>  16. |  2      6       1       0 |
>  17. |  2      7       1       0 |
>  18. |  2      8       1       0 |
>  19. |  2      9       0       0 |
>  20. |  2     10       0       0 |
>     |---------------------------|
>  21. |  3      1       0       0 |
>  22. |  3      2       1       1 |
>  23. |  3      3       0       0 |
>     +---------------------------+
>
> So, for ID 1, the first time at which state = 1 occurs is the fourth record,
> for
> ID 2 it is the third record etc. I want to assign a value within an id equal
> to
> that index. For example, for ID 1 I want a variable that equals 4 for all
> ten cases, for ID 2 a variable equal to 3 for all cases etc. Put
> differently,
> I want to assign to all cases within an id, the value of _n when first = 1.
> I can't seem to get my head around how to do this.
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```