Announcement

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

  • replace subsequent values

    Hi Stata experts,

    below is my dataset, what I am looking forward to is by ID, if duration=1 & duration[_n+1] ==0 & duration[_n+2] ==0, then replace the duration[_n+1] ==1 & duration[_n+2] ==1. Can you help me out of this? Thank you very much.

    clear
    input float(ID duration var3)
    1 30 1
    1 30 0
    1 20 0
    1 16 0
    2 35 0
    2 86 1
    2 24 0
    2 87 0
    3 23 0
    3 86 1
    3 60 1
    3 87 1
    3 34 0
    3 14 1
    3 67 1
    3 89 0
    3 43 0
    4 32 1
    4 54 1
    4 100 1
    4 58 1
    4 85 0
    4 34 0
    4 16 0
    end

    blow dataset is what I expect

    clear
    input float(ID duration var3)
    1 30 1
    1 30 1
    1 20 1
    1 16 0
    2 35 0
    2 86 1
    2 24 1
    2 87 1
    3 23 0
    3 86 1
    3 60 1
    3 87 1
    3 34 0
    3 14 1
    3 67 1
    3 89 1
    3 43 1
    4 32 1
    4 54 1
    4 100 1
    4 58 1
    4 85 1
    4 34 1
    4 16 0
    end

  • #2
    sorry, just to be clear, I only hope to replace the subsequent two 0(s) with 1 if period[_n]=1. Look forward to your guidance.

    Comment


    • #3
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(ID duration var3)
      1  30 1
      1  30 0
      1  20 0
      1  16 0
      2  35 0
      2  86 1
      2  24 0
      2  87 0
      3  23 0
      3  86 1
      3  60 1
      3  87 1
      3  34 0
      3  14 1
      3  67 1
      3  89 0
      3  43 0
      4  32 1
      4  54 1
      4 100 1
      4  58 1
      4  85 0
      4  34 0
      4  16 0
      end
      
      gen long obsno=_n
      bys ID (obsno): gen tag= (var3[_n-2] & !var3[_n-1])|(var3[_n-1] & !var3[_n+1])
      replace var3= tag if !var3 & tag
      Res.:

      Code:
      . l, sepby(ID)
      
           +------------------------------------+
           | ID   duration   var3   obsno   tag |
           |------------------------------------|
        1. |  1         30      1       1     1 |
        2. |  1         30      1       2     1 |
        3. |  1         20      1       3     1 |
        4. |  1         16      0       4     0 |
           |------------------------------------|
        5. |  2         35      0       5     0 |
        6. |  2         86      1       6     1 |
        7. |  2         24      1       7     1 |
        8. |  2         87      1       8     1 |
           |------------------------------------|
        9. |  3         23      0       9     0 |
       10. |  3         86      1      10     1 |
       11. |  3         60      1      11     0 |
       12. |  3         87      1      12     1 |
       13. |  3         34      0      13     0 |
       14. |  3         14      1      14     1 |
       15. |  3         67      1      15     1 |
       16. |  3         89      1      16     1 |
       17. |  3         43      1      17     1 |
           |------------------------------------|
       18. |  4         32      1      18     0 |
       19. |  4         54      1      19     0 |
       20. |  4        100      1      20     0 |
       21. |  4         58      1      21     1 |
       22. |  4         85      1      22     1 |
       23. |  4         34      1      23     1 |
       24. |  4         16      0      24     0 |
           +------------------------------------+

      Comment


      • #4
        Another way to do it.


        Code:
        clear
        input float(ID duration var3)
        1 30 1
        1 30 0
        1 20 0
        1 16 0
        2 35 0
        2 86 1
        2 24 0
        2 87 0
        3 23 0
        3 86 1
        3 60 1
        3 87 1
        3 34 0
        3 14 1
        3 67 1
        3 89 0
        3 43 0
        4 32 1
        4 54 1
        4 100 1
        4 58 1
        4 85 0
        4 34 0
        4 16 0
        end
        
        gen long pstime = _n 
        
        gen when1 = pstime if var3 == 1 
        
        bysort ID (pstime) : replace when = when[_n -1] if missing(when)
        
        clonevar wanted = var3 
        
        replace wanted = 1 if wanted == 0 & inlist(pstime - when, 1, 2)
        
        list, sepby(ID)
        
        
             +------------------------------------------------+
             | ID   duration   var3   pstime   when1   wanted |
             |------------------------------------------------|
          1. |  1         30      1        1       1        1 |
          2. |  1         30      0        2       1        1 |
          3. |  1         20      0        3       1        1 |
          4. |  1         16      0        4       1        0 |
             |------------------------------------------------|
          5. |  2         35      0        5       .        0 |
          6. |  2         86      1        6       6        1 |
          7. |  2         24      0        7       6        1 |
          8. |  2         87      0        8       6        1 |
             |------------------------------------------------|
          9. |  3         23      0        9       .        0 |
         10. |  3         86      1       10      10        1 |
         11. |  3         60      1       11      11        1 |
         12. |  3         87      1       12      12        1 |
         13. |  3         34      0       13      12        1 |
         14. |  3         14      1       14      14        1 |
         15. |  3         67      1       15      15        1 |
         16. |  3         89      0       16      15        1 |
         17. |  3         43      0       17      15        1 |
             |------------------------------------------------|
         18. |  4         32      1       18      18        1 |
         19. |  4         54      1       19      19        1 |
         20. |  4        100      1       20      20        1 |
         21. |  4         58      1       21      21        1 |
         22. |  4         85      0       22      21        1 |
         23. |  4         34      0       23      21        1 |
         24. |  4         16      0       24      21        0 |
             +------------------------------------------------+
        See also https://journals.sagepub.com/doi/pdf...6867X231196519 including Section 5.2.

        Comment

        Working...
        X