Announcement

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

  • creating "course of treatment" variable

    Hi everyone.

    I have a dataset in which I am trying to determine gaps between prescriptions and whether they are less than 90 days and therefore in the same "treatment course".

    For example:

    prescription gap (days)
    30 (1st course of treatment)
    45 (1st course of treatment)
    23 (1st course of treatment)
    89 (1st course of treatment)
    150 (more than 90 days - new course - 2nd course of treatment)
    34 (2nd course of treatment)
    23 (2nd course of treatment)
    300 (more than 90 days - 3rd course of treatment)
    45 (3rd course of treatment)

    and so on....

    I have tried the following commands:
    by patid: gen ct=1 if prsc_gap[_n+1]<=90
    by patid: replace ct=2 if prsc_gap>90 & prsc_gap[_n-1]>90

    but I know that this is probably the wrong way to go about this problem and am struggling to find an alternative.

    Does anyone know how I could approach this?

    Thanks a lot!

  • #2
    Evidently you have irregularly spaced panel data with a patient identifier patid. One way is just to work with the original dates and count how many observations there were in the previous 90 days for each patient. I use rangestat from SSC. You don't give a data example directly (do please read and act on FAQ Advice #12 to use dataex) so I invented one.

    Code:
    clear 
    set obs 20 
    set seed 2803 
    gen patid = cond(_n <= 10, 1, 2) 
    bysort patid : gen when = runiformint(1, 50)
    by patid : replace when = when[_n-1] + runiformint(1,120) if _n > 1 
    
    rangestat (count) previous=patid, int(when -90 -1) by(patid) 
    bysort patid (when) : gen course = sum(previous == .) 
    
    list, sepby(patid)
    Code:
         +----------------------------------+
         | patid   when   previous   course |
         |----------------------------------|
      1. |     1     22          .        1 |
      2. |     1     81          1        1 |
      3. |     1    130          1        1 |
      4. |     1    174          1        1 |
      5. |     1    223          1        1 |
      6. |     1    287          1        1 |
      7. |     1    308          2        1 |
      8. |     1    343          2        1 |
      9. |     1    443          .        2 |
     10. |     1    457          1        2 |
         |----------------------------------|
     11. |     2      6          .        1 |
     12. |     2     66          1        1 |
     13. |     2    183          .        2 |
     14. |     2    198          1        2 |
     15. |     2    231          2        2 |
     16. |     2    316          1        2 |
     17. |     2    323          1        2 |
     18. |     2    347          2        2 |
     19. |     2    371          3        2 |
     20. |     2    465          .        3 |
         +----------------------------------+


    While I like
    rangestat for various reasons, including its versatility for related problems, I'd note that

    Code:
    bysort patid (when) : gen course = sum((when - when[_n-1]) > 90) 
    is equivalent. This works for the first observation for each patient too as then a reference to when[0] is evaluated as missing and so is the expression compared with 90.

    Comment


    • #3
      That worked, thank you so much!

      Comment

      Working...
      X