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]

st: Capturing the date and which something first occurs


From   "Richard T. Campbell" <[email protected]>
To   "statalist-hsphsun2.harvard.edu" <[email protected]>
Subject   st: Capturing the date and which something first occurs
Date   Fri, 06 Apr 2012 12:28:39 -0500

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.

--
Richard T. Campbell
Professor Emeritus, School of Public Health
Institute for Health Research and Policy
University of Illinois at Chicago
Phone 312-413-0480

*
*   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/


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