Announcement

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

  • selecting min date by 2 grouping variables

    My dataset has 2 grouping variables: ID and drug name
    Also date for starting the treatment with a certain drug but per one drug can be several dates or the date is missing, I need to select the earliest date within a drug.
    Data looks like this:
    patient_id start_date drug
    1562 07.01.2019 drug1
    1562 15.04.2019 drug1
    1562 15.04.2019 drug1
    1562 15.04.2019 drug1
    1562 18.03.2020 drug1
    1562 . drug1
    1562 18.03.2020 drug1
    1562 08.11.2022 drug2
    1562 08.11.2022 drug2
    1562 21.02.2023 drug3
    I know command bysort id : egen mindate = min(start_date)
    But this does not work for me because one person may have multiple starting dates when starting with a new drug. I would be very grateful if anyone could help.

  • #2
    Code:
    bysort id drug (start_date): gen min_date_drug = start_date[1]
    egen is sometimes needed for better treatment of missing values, and so forth, but here that is unlikely to bite.

    Comment


    • #3
      I got the error: weights not allowed

      Comment


      • #4
        You probably included a space in the code between start_date and the square brackets, which stata interprets as the option for weights. The code as Nick wrote runs well.

        Comment


        • #5
          Yes, I had a space and now it works. Thank you very much to both of you

          Comment

          Working...
          X