Announcement

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

  • How to Fix the Data Error within ID in Panel Data using Stata?

    I have a small dataset and the "lep" variable has some errors. I want to fix the errors with the rule below,
    Within ID, if the consecutive zeros occur in the lep variable, then the zeros before the consecutive zeros should be equal 0.
    For instance, when ID=1 and position=8, and 9, lep=0 for these 2 positions, which is completely correct.
    However, the lep variable equal 0 when position=4 and position=6, respectively, which is wrong.
    They should be corrected as 1, respectively. For other IDs, the similar situation should be changed as well----like ID=4, when the position=7, the lep=0 should be changed 1.

    *Simulated data for illustrative purpose.
    clear
    input byte (id lep position)
    1 1 1
    1 1 2
    1 1 3
    1 0 4
    1 1 5
    1 0 6
    1 1 7
    1 0 8
    1 0 9
    2 1 1
    2 1 2
    2 0 3
    2 1 4
    2 0 5
    2 0 6
    2 0 7
    3 1 1
    3 0 2
    3 1 3
    3 0 4
    3 0 5
    4 1 6
    4 0 7
    4 1 8
    4 0 9
    end

    Thank you!
    Last edited by smith Jason; 21 Mar 2022, 23:43.

  • #2
    Code:
    by id (pos), sort: replace lep=1 if lep[_n-1]==1 & lep[_n+1]==1

    Comment


    • #3
      Thank you very much!

      Comment


      • #4
        Originally posted by Øyvind Snilsberg View Post
        Code:
        by id (pos), sort: replace lep=1 if lep[_n-1]==1 & lep[_n+1]==1
        I think either lep[_n-1]==1 or lep[_n+1]==1 can be replaced by 1 directly and the code still can work.

        Comment

        Working...
        X