Announcement

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

  • Time-in treatment for panel data

    Hi - I am trying to create a time-varying variable that identifies how long a person has been receiving intervention (i.e., time between assessment 1 and assessment 2; time between assessment 1 and assessment 3, time between assessment 1 and assessment 4, etc.)
    ID Score Assessment Age (months) Time receiving intervention
    1 20 1 18 0
    2 30 1 20 0
    2 35 2 26 *Should be 6 (26-20 mos.)
    3 15 1 24 0
    4 7 1 30 0
    5 10 1 26 0
    5 16 2 30 *Should be 4 (30-26 mos.)
    5 18 3 37 *Should be 11 (37-26 mos.)
    6 40 4 42 *Should be 16 (42-26 mos.)

    Thank you so much in advance for your ideas!

  • #2
    BJ:
    assuming you have no missing data, you can try:
    Code:
    bysort id: g A=agemonths[_n]- agemonths[_n-1]
    replace A=0 if A==.
    bysort id: g B=sum(A)
    Last edited by Carlo Lazzaro; 21 Feb 2017, 08:52.
    Kind regards,
    Carlo
    (Stata 18.0 SE)

    Comment


    • #3
      Thank you Carlo! I tried the code you sent, and it works really well for calculating the time between assessment 1 and assessment 2, but it doesn't calculate the time between assessment 1 and assessment 3. Instead it gives me the difference between assessment 1 and assessment 2, then the difference between assessment 2 and assessment 3. Thanks so much for any other ideas you may have! BJ

      Comment


      • #4
        BJ: Please note the advice on how to present data in http://www.statalist.org/forums/help#stata

        and then consider the following. Your last observation looks wrong to me in some detail or another, probably that the identifier should really be 5.

        Code:
        clear
        input ID    Score    Assessment    Age  Wanted
        1    20    1    18    0    
        2    30    1    20    0    
        2    35    2    26    6
        3    15    1    24    0    
        4    7    1    30    0    
        5    10    1    26    0    
        5    16    2    30    4
        5    18    3    37    11
        6    40    4    42    16
        end
        
        bysort ID : gen Suggested = Age - Age[1]
        
        list, sepby(ID)
        
             +-------------------------------------------------+
             | ID   Score   Assess~t   Age   Wanted   Sugges~d |
             |-------------------------------------------------|
          1. |  1      20          1    18        0          0 |
             |-------------------------------------------------|
          2. |  2      30          1    20        0          0 |
          3. |  2      35          2    26        6          6 |
             |-------------------------------------------------|
          4. |  3      15          1    24        0          0 |
             |-------------------------------------------------|
          5. |  4       7          1    30        0          0 |
             |-------------------------------------------------|
          6. |  5      10          1    26        0          0 |
          7. |  5      16          2    30        4          4 |
          8. |  5      18          3    37       11         11 |
             |-------------------------------------------------|
          9. |  6      40          4    42       16          0 |
             +-------------------------------------------------+

        Comment


        • #5
          This works perfectly. Thank you so much! BJ

          Comment

          Working...
          X