Hello,

I have a state-year panel dataset, which is mostly balanced except for one missing year in Wisconsin (1998) and two missing years in Maine (1991 and 1992). For a set of 3 outcomes, I'm hoping to replace Wisconsin's missing values with the average of that state's values in 1997 and 1999, and Maine's missing values for both years 1991 and 1992 with the average of its values in 1990 and 1993. I have used the code below for the case of Wisconsin when there are non-missing values immediately surrounding one year of missing values:

Code:
bysort state (year) : replace `v' = (`v'[_n-1] + `v'[_n+1])/2 if (missing(`v')
But that code does not work in the case of Maine since each missing year has another missing value adjacent to it. I'd like to write out code like the following but it does not work. Can anyone suggest a more flexible code like the one below or offer a modification to the above code for the case of Maine's missing 1991 and 1992 values?


Code:
foreach v in outcome1 outcome2 outcome3 {
    bysort state (year) : replace `v' = (`v'[1997] + `v'[1999])/2 if state=="wisconsin" & year==1998
    bysort state (year) : replace `v' = (`v'[1990] + `v'[1993])/2 if state=="maine" & year==1991
    bysort state (year) : replace `v' = (`v'[1990] + `v'[1993])/2 if state=="maine" & year==1992
}
Thank you very much for your time!

Tom