Announcement

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

  • Implementing Month-Specific Treatment Duration Carryover in Panel Data

    Hi everyone,

    I'm working with a panel dataset where each row corresponds to a monthly observation for a subject. One of the variables, months_on_treatment, indicates the month when a treatment was prescribed and shows the duration of the treatment in months. The column months_fu refers to the current observation in months.

    Here's my current problem: I need to carry over the treatment duration data only for the months where the treatment is actually supposed to be active, post-prescription. However, I am struggling to apply this only to the relevant months for each subject after the initial prescription observation.

    Could someone help me devise a Stata code or suggest a method to correctly propagate the treatment duration data across the relevant months?


    This is an example of how my data looks:
    id treat_duration months_fu
    1 5 0
    1 . 1
    1 . 2
    1 . 3
    1 . 4
    1 . 5
    1 . 6
    1 . 7
    1 . 8
    1 . 9
    1 4 10
    1 . 11
    1 . 12
    1 . 13
    1 . 14
    1 , 15
    1 . 16
    1 . 17
    1 . 18
    1 . 19
    1 . 20
    1 3 21
    1 . 22
    1 . 23
    1 . 24
    1 . 25
    What I need is to generate the "on_treatment" variable:
    id treat_duration months_fu on_treatment
    1 5 0 yes
    1 . 1 yes
    1 . 2 yes
    1 . 3 yes
    1 . 4 yes
    1 . 5 no
    1 . 6 no
    1 . 7 no
    1 . 8 no
    1 . 9 no
    1 4 10 yes
    1 . 11 yes
    1 . 12 yes
    1 . 13 yes
    1 . 14 no
    1 , 15 no
    1 . 16 no
    1 . 17 no
    1 . 18 no
    1 . 19 no
    1 . 20 no
    1 3 21 yes
    1 . 22 yes
    1 . 23 yes
    1 . 24 no
    1 . 25 no
    Thank you in advance for your help!
    Last edited by Sergio Serrano; 03 May 2024, 06:07.

  • #2
    Hi Sergio,

    the following code should work for your snippet of data and I hope it also will for your whole dataset:

    Code:
    clear
    input int (id     treat_duration     months_fu)
    1     5     0
    1     .     1
    1     .     2
    1     .     3
    1     .     4
    1     .     5
    1     .     6
    1     .     7
    1     .     8
    1     .     9
    1     4     10
    1     .     11
    1     .     12
    1     .     13
    1     .     14
    1     ,     15
    1     .     16
    1     .     17
    1     .     18
    1     .     19
    1     .     20
    1     3     21
    1     .     22
    1     .     23
    1     .     24
    1     .     25
    end
    
    
    gen on_treatment = treat_duration + months_fu -1
    bysort id (months_fu): replace on_treatment = on_treatment[_n-1] if !missing(on_treatment[_n-1]) & on_treatment[_n-1]>=months_fu
    recode on_treatment (.=0)(else=1)
    label define yn 1 "yes" 0 "no"
    label values on_treatment yn
    All the best,
    Benno

    Comment


    • #3
      It works perfectly, very elegant!
      I was stuck with this. Thank you so much, Benno.

      Comment

      Working...
      X