Dear Statalisters,
I am a newbie to stata and pretty sure that my problem can be solved quite easily. But I´ve been trying to solve it for hours and I didn`t succeed
I have panel data that looks like this:
For some values, y2000 is missing. I want to replace the missing value with the closest neighbor value, but within one country, i.e., within a row. E.g., for country_code a, y2000 should be 20.
I tried this:
foreach y of varlist y1960-y2016 {
bysort country_code: replace y2000 = `y' if y2000==.
}
It works fine, but of course stata takes always the earliest value available. In my example, it would use 10 instead of 20, although 2016 is closer to 2000 than 1970.
I tried several things, but I just cant figure it out.
It would be great if you could help me
I am a newbie to stata and pretty sure that my problem can be solved quite easily. But I´ve been trying to solve it for hours and I didn`t succeed

I have panel data that looks like this:
country_code | y1970 | y2000 | y2016 |
a | 10 | . | 20 |
b | 5 | . | 10 |
c | 10 | 3 | 4 |
I tried this:
foreach y of varlist y1960-y2016 {
bysort country_code: replace y2000 = `y' if y2000==.
}
It works fine, but of course stata takes always the earliest value available. In my example, it would use 10 instead of 20, although 2016 is closer to 2000 than 1970.
I tried several things, but I just cant figure it out.
It would be great if you could help me
Comment