Announcement

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

  • Generate variable based on previous value for other group variable

    id t d occur t0 t_
    1 0 0 0 - -
    1 1 0 0 - -
    1 2 1 1 0 2
    1 3 1 2 2 3
    1 4 0 2 - -
    1 5 0 2 - -
    1 6 1 3 3 6


    Hi,

    I'm trying to prepare my panel data for a survival analysis and now trying to generate the t0 variable indicating the starting time for the analysis.

    t: time
    d:
    status variable indicating whether individual been through event or not
    occur: accumulated event
    t0: analysis time when record begins
    t_ : analysis time when record ends

    So I'm trying to generate t0 variable: when id have experienced event (d=1), t0 is then suppose to obtain the minimum value of t in the previous group of occur
    That is how I'm thinking of solving this problem but I cannot manage to solve it! and btw. I am going to delete the rows with the value - after generating all my variables.


    (I have sensitive data so I cannot share an example)


    Thanks!

  • #2
    This will give you what you want:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(id t d occur) str1(t0 t_)
    1 0 0 0 "-" "-"
    1 1 0 0 "-" "-"
    1 2 1 1 "0" "2"
    1 3 1 2 "2" "3"
    1 4 0 2 "-" "-"
    1 5 0 2 "-" "-"
    1 6 1 3 "3" "6"
    end
    
    bys  id (t): gen seq= 1+ sum(d>0)
    bys seq: egen min_t= min(t)
    bys  id (t): gen wanted= min_t[_n-1] if d
    Res.:

    Code:
    . l, sep(0)
    
         +-----------------------------------------------------+
         | id   t   d   occur   t0   t_   seq   min_t   wanted |
         |-----------------------------------------------------|
      1. |  1   0   0       0    -    -     1       0        . |
      2. |  1   1   0       0    -    -     1       0        . |
      3. |  1   2   1       1    0    2     2       2        0 |
      4. |  1   3   1       2    2    3     3       3        2 |
      5. |  1   4   0       2    -    -     3       3        . |
      6. |  1   5   0       2    -    -     3       3        . |
      7. |  1   6   1       3    3    6     4       6        3 |
         +-----------------------------------------------------+
    
    .

    Comment


    • #3
      Originally posted by Andrew Musau View Post
      This will give you what you want:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input byte(id t d occur) str1(t0 t_)
      1 0 0 0 "-" "-"
      1 1 0 0 "-" "-"
      1 2 1 1 "0" "2"
      1 3 1 2 "2" "3"
      1 4 0 2 "-" "-"
      1 5 0 2 "-" "-"
      1 6 1 3 "3" "6"
      end
      
      bys id (t): gen seq= 1+ sum(d>0)
      bys seq: egen min_t= min(t)
      bys id (t): gen wanted= min_t[_n-1] if d
      Res.:

      Code:
      . l, sep(0)
      
      +-----------------------------------------------------+
      | id t d occur t0 t_ seq min_t wanted |
      |-----------------------------------------------------|
      1. | 1 0 0 0 - - 1 0 . |
      2. | 1 1 0 0 - - 1 0 . |
      3. | 1 2 1 1 0 2 2 2 0 |
      4. | 1 3 1 2 2 3 3 3 2 |
      5. | 1 4 0 2 - - 3 3 . |
      6. | 1 5 0 2 - - 3 3 . |
      7. | 1 6 1 3 3 6 4 6 3 |
      +-----------------------------------------------------+
      
      .
      Thank you so much it worked wonderful !!

      Comment

      Working...
      X