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')
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 }
Tom
0 Response to Imputing missing state-years using average of surrounding years
Post a Comment