Announcement

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

  • Identifying patient-IDs with two variables in common

    I have a dataset with many subjects (patient-IDs) with many observations per patient. I would like to find out how many patients that got medicine 1 (med1) each year. In the example dataset med1 is given three times in 2012, but only to two different patients.
    I tried
    bys PatientID (med1): gen med1pat=!mi(med1[1])
    to first identify all patients that got med1 at any time.

    Then
    by PatientID: gen med1pat_2012=1 if med1==1 & year==2012
    by PatientID: gen uniqu_med1pat_2012=1 if _n==1 & med1pat_2012==1
    by PatientID: gen med1pat_2013=1 if med1==1 & year==2013
    by PatientID: gen uniqu_med1pat_2013=1 if _n==1 & med1pat_2013==1

    This gave me the right answer for 2012, med1 given three times (tab med1pat_2012: 3) and to two specific patients (tab unique_med1pat_2012: 2)
    But for 2013 it got wrong...

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte PatientID int year byte med1 float(med1pat uniqu_med1pat med1pat_2012 uniqu_med1pat_2012 med1pat_2013 uniqu_med1pat_2013)
    1 2012 1 1 1 1 1 . .
    1 2014 1 1 . . . . .
    1 2012 1 1 . 1 . . .
    1 2013 . 1 . . . 1 .
    1 2015 . 1 . . . . .
    2 2013 1 1 1 . . 1 1
    2 2014 1 1 . . . . .
    2 2014 1 1 . . . . .
    2 2015 . 1 . . . . .
    2 2012 . 1 . 1 . . .
    3 2012 . 0 . . . . .
    3 2014 . 0 . . . . .
    3 2015 . 0 . . . . .
    3 2013 . 0 . . . . .
    4 2012 1 1 1 1 1 . .
    4 2013 1 1 . . . 1 .
    4 2015 1 1 . . . . .
    4 2015 1 1 . . . . .
    4 2014 . 1 . . . . .
    end
    Thank you for helping.

  • #2
    Cathrine:
    do you mean something along the following lines?
    Code:
    . bysort PatientID year: egen wanted=sum( med1)
    
    . list PatientID year med1 wanted
    
         +---------------------------------+
         | Patien~D   year   med1   wanted |
         |---------------------------------|
      1. |        1   2012      1        2 |
      2. |        1   2012      1        2 |
      3. |        1   2013      .        0 |
      4. |        1   2014      1        1 |
      5. |        1   2015      .        0 |
         |---------------------------------|
      6. |        2   2012      .        0 |
      7. |        2   2013      1        1 |
      8. |        2   2014      1        2 |
      9. |        2   2014      1        2 |
     10. |        2   2015      .        0 |
         |---------------------------------|
     11. |        3   2012      .        0 |
     12. |        3   2013      .        0 |
     13. |        3   2014      .        0 |
     14. |        3   2015      .        0 |
     15. |        4   2012      1        1 |
         |---------------------------------|
     16. |        4   2013      1        1 |
     17. |        4   2014      .        0 |
     18. |        4   2015      1        2 |
     19. |        4   2015      1        2 |
         +---------------------------------+
    
    .
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      perhaps,
      Code:
      egen tag = tag(PatientID year) if med1==1
      bys year: egen wanted = total(tag)

      Comment

      Working...
      X