Hi Statalist,

I have a time series dataset of ozone data for multiple different counties. It has quite a bit of missing data, sometimes several observations in a row. I want to fill in the missing data using the average of the nearest non-missing observation prior and the nearest non-missing observation afterwards in that county.
County Week Ozone
Milwaukee 532 0.148
Milwaukee 533 .
Milwaukee 534 .
Milwaukee 535 0.564
Waukesha 127 0.185
Waukesha 128 .

In this data, I would want both week 533 and week 534 in Milwaukee County to be 0.356 (the average of 0.148 and 0.564). I have tried lots of different codes but they are either not able to be used with "by" commands (necessary because it must be done by county) or it includes other observations that are missing in the average instead of averaging the two nearest non-missing. Any thoughts on a code that would work here?

Thanks!