Announcement

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

  • Fill in missing value in panel based on preceding & following values

    Hi Statalist

    I have a panel of the following structure

    . list, sepby(Id)

    +--------------------+
    | Id Year parent |
    |--------------------|
    1. | 1 2007 Yes |
    2. | 1 2008 Yes |
    3. | 1 2009 Yes |
    4. | 1 2010 . |
    5. | 1 2011 Yes |
    |--------------------|
    6. | 2 2007 No |
    7. | 2 2008 No |
    8. | 2 2009 . |
    9. | 2 2010 No |
    10. | 2 2011 No |
    |--------------------|
    11. | 3 2007 No |
    12. | 3 2008 No |
    13. | 3 2009 No |
    14. | 3 2010 . |
    15. | 3 2011 Yes |
    +--------------------+


    What I would like to do is to fill in, wherever possible, the missing values for the parent indicator based on the preceding AND following values.

    That is, for Id 1, . should be replaced with "Yes". For Id 2, replaced with "No". But for Id 3, it should remain missing because it is not possible to determine if 2010 is the first year during which the respondent became a parent.

    I have tried
    Code:
    . bysort Id (Year): replace parent = parent[_n-1] if parent == .
    but am not able to create the conditions of "Check both its preceding & following values are not missing, and only if they are identical, replace . with that value".

    Thanks

  • #2
    Code:
    bysort Id (Year): replace Parent = Parent[_n-1] if missing(Parent] & Parent[_n-1] == Parent[_n+1]
    note that replacing missings by missings is
    not a problem.

    Comment


    • #3
      Oh I see! Thank you very much Nick.

      Comment

      Working...
      X