Announcement

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

  • How to replace a value taking the mean of two other variables?

    Hi everyone,
    I am working with a panel data set, 4 waves, long format.
    I created a dummy for missing values, if respondents participated in the wave, but not answered the question (dummy_`var')
    Now I want to replace the missing values in each wave with the mean of the `var'[_n-1] and `var'[_n+1] if dummy_`var'==1
    The variable names are: cesd numphy
    This is the command I used:

    foreach var in cesd numphy {

    replace `var' = `var'[_n-1], `var'[_n+1]) / 2 if dummy_`var'==1

    }

    Somehow stata replies with "option cesd not allowed".

    Does anyone have any idea what I did wrong and how I can make it work?

    Kind Regards,
    Lisa

  • #2
    That is fantasy syntax. I don't know any language in which something like that would work as you wish. Try

    Code:
    replace `var' = (`var'[_n-1] + `var'[_n+1]) / 2 if dummy_`var'==1

    Comment


    • #3
      It worked. Thanks a lot

      Comment

      Working...
      X