Announcement

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

  • Help creating a variable

    Dear community,

    I am trying to create a variable called polypharmacy, which identifies participants in a cohort who have SIMULTANEOUSLY received at least 4 drugs during their follow-up.

    In my database, the variable ID identifies the participants, and each participant has as many lines as drugs have been prescribed. The variable t_sd identifies the start date and the variable t_ed identifies the end date of each drug.

    How could I identify the first date with simultaneous use of at least 4 drugs?

    Many thanks in advance!

  • #2
    Please give a data example as explained at https://www.statalist.org/forums/help#stata

    As mentioned there, realistic data are fine if real data are confidential or sensitive.

    I am guessing a little bit, but here is a sandbox. I imagine your real dates are daily or monthly, but the principles are generic.

    The problem is hard for me with your layout, but easier with a trick as explained at https://journals.sagepub.com/doi/pdf...867X1301300116

    Here is some technique. The variable produced counts the number of drugs through adding 1 when a drug is started and -1 when it is ended. An indicator for 4 or more follows easily.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float id str1 drug float(start end)
    1 "A" 2000 2003
    1 "B" 2002 2004
    1 "C" 2008 2009
    1 "D" 2010 2011
    2 "A" 2001 2003
    2 "B" 2002 2007 
    2 "C" 2003 2008 
    2 "D" 2003 2007
    2 "E" 2008 2010
    2 "F" 2009 2012
    end
    
    expand 2 
    bysort id drug : gen change = cond(_n == 1, 1, -1)
    by id drug : gen date = cond(_n == 1, start, end)
    
    gsort id date -change 
    bysort id (date) : gen count = sum(change)
    
    list, sepby(id)
    
         +--------------------------------------------------+
         | id   drug   start    end   change   date   count |
         |--------------------------------------------------|
      1. |  1      A    2000   2003        1   2000       1 |
      2. |  1      B    2002   2004        1   2002       2 |
      3. |  1      A    2000   2003       -1   2003       1 |
      4. |  1      B    2002   2004       -1   2004       0 |
      5. |  1      C    2008   2009        1   2008       1 |
      6. |  1      C    2008   2009       -1   2009       0 |
      7. |  1      D    2010   2011        1   2010       1 |
      8. |  1      D    2010   2011       -1   2011       0 |
         |--------------------------------------------------|
      9. |  2      A    2001   2003        1   2001       1 |
     10. |  2      B    2002   2007        1   2002       2 |
     11. |  2      C    2003   2008        1   2003       3 |
     12. |  2      D    2003   2007        1   2003       4 |
     13. |  2      A    2001   2003       -1   2003       3 |
     14. |  2      B    2002   2007       -1   2007       2 |
     15. |  2      D    2003   2007       -1   2007       1 |
     16. |  2      E    2008   2010        1   2008       2 |
     17. |  2      C    2003   2008       -1   2008       1 |
     18. |  2      F    2009   2012        1   2009       2 |
     19. |  2      E    2008   2010       -1   2010       1 |
     20. |  2      F    2009   2012       -1   2012       0 |
         +--------------------------------------------------+
    
    .

    Comment


    • #3
      Thank you for your reply. Here is the data example (missing value means drug is still in use)


      Code:
      clear
      input float(id drug start end)
       1  2 21416     .
       1 28 21431 22971
       1  1 22187 22971
       2 28 21531     .
       2 25 21714     .
       6  2 21001     .
       6 33 21059     .
       6  3 21564     .
       9  2 22698     .
      10  3 21355     .
      10 28 21944     .
      10 34 21944     .
      10  2 22747     .
      12  7 20048 21084
      12  6 20048 20977
      13  4 19647     .
      13 28 20803     .
      13 21 20909     .
      14 36 19239     .
      14 28 21928     .
      end
      format %dCY-N-D start
      format %dCY-N-D end

      Comment


      • #4
        Thank you very much for the Stata journal tip. I have managed to create the variable correctly.

        Thank you!

        Comment

        Working...
        X