Announcement

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

  • drop observations if certain condition

    Hi

    How to drop whole "person_id" if for the same treatment there is NOT time = 1 & time = 3 & time = 6 & time = 12 & time = 18? Please see example below:

    Code:
    person_id       treatment           time
          1                 A             1
          1                 A             3
          1                 A             6
          1                 A             12
          1                 A             18
          2                 B             1
          2                 C             3
          2                 C             6
          2                 C             18
          3                 A             6
    In this case, I would like to have (dropping all the other observations):

    Code:
    person_id        treatment            time
          1                 A             1
          1                 A             3
          1                 A             6
          1                 A             12
          1                 A             18
    Thanks!!

  • #2

    Code:
    egen wanted = total(inlist(time, 1, 3, 6, 12, 18)), by(person_id) 
    
    keep if wanted == 5

    Comment


    • #3
      Thank you very much Nick for your quick reply. How to account for the following scenario?

      Code:
      person_id            treatment                time
            1                    A                    3
            1                    A                    6
            1                    A                    12
            1                    B                    1
            1                    B                    18

      In this case, person_id has 1,3,6,12,18 but for different treatments. So, in this case I want to drop observations. I need 1,3,6,12,18 for the same treatment.

      Thanks a lot!
      Last edited by Sofia Oliveira; 06 May 2019, 08:05.

      Comment


      • #4
        My error, but clearly you need

        Code:
         
         egen wanted = total(inlist(time, 1, 3, 6, 12, 18)), by(person_id treatment)

        Comment


        • #5
          Thank you very much, it works perfectly!

          Comment

          Working...
          X