Announcement

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

  • fill in values until next identical value (paneldata)

    Hi everybody

    I need to fill in some values. Here is an example dataset.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(time id education wanted)
     1 9999  .  .
     2 9999  .  .
     3 9999  .  .
     4 9999 20 20
     5 9999  . 20
     6 9999  . 20
     7 9999  . 20
     8 9999  . 20
     9 9999  . 20
    10 9999 20 20
    11 9999  .  .
    12 9999  .  .
     1 1000  .  .
     2 1000 25 25
     3 1000  . 25
     4 1000  . 25
     5 1000  . 25
     6 1000 25 25
     7 1000  .  .
     8 1000  .  .
     9 1000 30 30
    10 1000  . 30
    11 1000 30 30
    12 1000  .  .
    end

    The first observation in education is when the individual starts an education and the next observation in education is when the individual ends the education. I need to fill in the rows between the first and the second observation, thus indicating the entire period of an educational program. So, what I need is the variabel called wanted.

    I have looked into this but unfortunately I cannot make it work.

    Best
    Gutav

  • #2
    https://journals.sagepub.com/doi/pdf...6867X231196519 discusses precisely this problem in its Section 5.1.

    Comment


    • #3
      This might do it. I used the carryforward command to identify and fill last observation(s). It can be installed from ssc install carryforward.

      Code:
      gen byte mis = missing(education)
      bysort id mis (time): gen last = 1 if mis == 0 & _n == _N
          bysort id (time): carryforward last, replace
      
      by id (time): replace education = education[_n-1] if education == . & education[_n-1] !=. & missing(last)

      Comment


      • #4
        Hi both

        The code from Section 5.1. did the trick! Thank you!

        I also ran the code you provided Wei, and it looks like, in row 7 and 8, it carried over the education from the previous row.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input float(time id education wanted) byte mis float last
         1 1000  .  . 1 .
         2 1000 25 25 0 .
         3 1000 25 25 1 .
         4 1000 25 25 1 .
         5 1000 25 25 1 .
         6 1000 25 25 0 .
         7 1000 25  . 1 .
         8 1000 25  . 1 .
         9 1000 30 30 0 .
        10 1000 30 30 1 .
        11 1000 30 30 0 1
        12 1000  .  . 1 1
         1 9999  .  . 1 .
         2 9999  .  . 1 .
         3 9999  .  . 1 .
         4 9999 20 20 0 .
         5 9999 20 20 1 .
         6 9999 20 20 1 .
         7 9999 20 20 1 .
         8 9999 20 20 1 .
         9 9999 20 20 1 .
        10 9999 20 20 0 1
        11 9999  .  . 1 1
        12 9999  .  . 1 1
        end

        Comment

        Working...
        X