Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Picking up fist observation in long file where one date is after another


    Hello


    I would be very grateful for your advice. My data is in long format. I would like to keep the first observation only where ExamDate occurs after dob. I generated "date" which =. if dob precedes ExamDate. "n" just indexes id ("number") for each time point. In the data below, I would like to keep lines 2 and 11.

    Many thanks!




    HTML Code:
    [HTML][CODE]      | number   ExamDate~B         dob   date   n |
          |--------------------------------------------|
       1. |  11003    9/28/1995   02 Apr 96      .   1 |
       2. |  11003    4/18/1996   02 Apr 96      1   2 |
       3. |  11003    10/9/1996   02 Apr 96      1   3 |
       4. |  11003    4/17/1997   02 Apr 96      1   4 |
       5. |  11003     5/8/1998   02 Apr 96      1   5 |
       6. |  11003    3/15/1999   02 Apr 96      1   6 |
       7. |  11003    3/23/2000   02 Apr 96      1   7 |
       8. |  11003    3/16/2001   02 Apr 96      1   8 |
       9. |  11003     7/9/2004   02 Apr 96      1   9 |
      10. |  11005    9/27/1995   16 Apr 96      .   1 |
      11. |  11005     5/7/1996   16 Apr 96      1   2 |
      12. |  11005   10/16/1996   16 Apr 96      1   3 |
      13. |  11005    4/21/1997   16 Apr 96      1   4 |
      14. |  11005     4/2/1998   16 Apr 96      1   5 |
      15. |  11005    3/16/1999   16 Apr 96      1   6 |
      16. |  11005    3/23/2000   16 Apr 96      1   7 |
      17. |  11005    3/13/2001   16 Apr 96      1   8 |
    
    [/CODE]
    [/HTML]


  • #2
    It is probably unnecessary to create the variables "date" and "n" if the sole purpose of doing this is to drop observations that satisfy your condition. However, having done that, it appears that all you need is to drop the observation that immediately follows date==. which you can do as follows

    Code:
    bys number (n): keep if date[_n-1]==. &date[_n+1]!=. & !missing(date)
    For a more general method to specify your condition without having to generate extra variables, use dataex to present a data example. In this case, for example,

    Code:
    dataex in 1/17
    Last edited by Andrew Musau; 25 Jun 2018, 08:04.

    Comment

    Working...
    X