Announcement

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

  • Select patients with observations that contain variables with specific values

    Hi,

    I am new to stata. I have tried to find the answer to my question in FAQ but I didn't find exactly what I wanted, so I post it here.

    I am working with a database with different observationssome of them from the same patients. Each observation is a test and has a date and a result. I would like to select patients that have with one test performed at a specific date and another one after another date. Each patient could have more than one test that meets the criteria.

    id testdate testresult
    1 01jan2022 4000
    1 09sept2022 3000
    1 10sept2022 3050
    2 03jan2022 4000
    2 25sept2022 3000
    3 28sept2022 3050
    4 02feb2022 1500
    4 25sept2022 3000


    First, I tried creating a dummy variable to select identifiy if the test meet the criteria:

    generate dateselect= 1 if (tesdate>date("January 1 2022","MDY"))

    replace dateselect=2 if (tesdate>date("September 1 2022","MDY"))

    replace dateselect=0 if dateslect==.

    and do a reshape but as dateselect could have the same value in several observations from the same patient, it didn't work.

    Could some help me out?

    Thank you very much

  • #2
    See the FAQ https://www.stata.com/support/faqs/d...ble-recording/

    Code:
    egen crit1 = max(inrange(testdate, mdy(1,1,2022), .)), by(id) 
    
    egen crit2 = max(inrange(testdate, mdy(9,1,2022), .)), by(id) 
    
    gen found = crit1 & crit2
    Two details:

    1. I am guess that you really want on or after 1 January or 1 September. But if you really do want on or after 2 January or 2 September, modify accordingly.

    2. A date of missing qualifies as greater than any known date, but the inrange() function traps such cases.

    Comment


    • #3
      Thank you very much for your help! It worked perfectly.

      Comment

      Working...
      X