Announcement

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

  • Flagging if an event occured before another event in panel data.

    Hi there,

    I am working with panel data and would like to be able to flag if, for each individual in the dataset, opioids were prescribed before anticonvulsants. I have dates and order of prescription, but would like to create an indicator variable that tells me whether opioids were prescribed first (e.g.: 'opioid_first'==1 if yes; ==0 if not). Any help is greatly appreciated.

    Data example below:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte claimant_id strL(medicine_type date_dispensed) float order
    10 "opioid"         "10feb2014" 1
    10 "irrelevantdrug" "12feb2013" 2
    10 "anticonvulsant" "14feb2014" 3
    12 "anticonvulsant" "1jan2015"  1
    12 "opioid"         "2jan2015"  2
    12 "opioid"         "5jan2015"  3
    20 "irrelevantdrug" "20dec2016" 1
    20 "irrelevantdrug" "21dec2016" 2
    20 "irrelevantdrug" "22dec2016" 3
    20 "opioid"         "24dec2016" 4
    21 "anticonvulsant" "10oct2017" 1
    21 "opioid"         "11oct2017" 2
    21 "anticonvulsant" "12oct2017" 3
    21 "opioid"         "13oct2017" 4
    end




  • #2
    Giovanni:
    if your goal is to flag -opioid- when it is the first prescribed drug ever, you may want to try:
    Code:
    . bysort claimant_id ( order): g wanted=1 if medicine_type=="opioid" & order==1
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Thanks very much Carlo. There are instances in the data where opioid won't be order==1. Patients may have been prescribed several other medications so -order- could be any number. But I still need to flag whether they were prescribed before anticonvulsants. Thanks.
      Last edited by Giovanni Ferreira; 04 Apr 2023, 01:44.

      Comment


      • #4
        There is quite a lot to do here even for this easy-to-state problem.

        1. Create numeric date variables.

        2. Find first dates and make them comparable.

        3. Cope with possibilities such as one drug prescribed but not the other; both prescribed same first day; neither prescribed.

        Here is one approach. See also Section 9 in https://www.stata-journal.com/articl...article=dm0055

        Code:
        clear
        input byte claimant_id strL(medicine_type date_dispensed) float order
        10 "opioid"         "10feb2014" 1
        10 "irrelevantdrug" "12feb2013" 2
        10 "anticonvulsant" "14feb2014" 3
        12 "anticonvulsant" "1jan2015"  1
        12 "opioid"         "2jan2015"  2
        12 "opioid"         "5jan2015"  3
        20 "irrelevantdrug" "20dec2016" 1
        20 "irrelevantdrug" "21dec2016" 2
        20 "irrelevantdrug" "22dec2016" 3
        20 "opioid"         "24dec2016" 4
        21 "anticonvulsant" "10oct2017" 1
        21 "opioid"         "11oct2017" 2
        21 "anticonvulsant" "12oct2017" 3
        21 "opioid"         "13oct2017" 4
        end
        
        gen ddate = daily(date_dispensed, "DMY")
        
        bysort claimant_id : egen first_opioid = min(cond(medicine_type == "opioid", ddate, .)) 
        bysort claimant_id : egen first_anti = min(cond(medicine_type == "anticonvulsant", ddate, .)) 
        
        gen which_first = sign(first_anti - first_opioid) if !missing(first_anti, first_opioid)
        label def which_first 1 "opioid" 0 "together" -1 "anti"
        label val which_first which_first 
        
        gen indicator = first_opioid < first_anti if !missing(first_anti, first_opioid)
        label def indicator 1 "opioid first" 0 "other"
        label val indicator indicator 
        
        format first_* %td 
        tabdisp claimant_id, c(first* which_first indicator)
        
        ------------------------------------------------------------------
        claimant_ |
        id        | first_opioid    first_anti   which_first     indicator
        ----------+-------------------------------------------------------
               10 |    10feb2014     14feb2014        opioid  opioid first
               12 |    02jan2015     01jan2015          anti         other
               20 |    24dec2016                                          
               21 |    11oct2017     10oct2017          anti         other
        ------------------------------------------------------------------

        Comment


        • #5
          It worked perfectly Nick, thanks so much!

          Comment

          Working...
          X