Hi,
I have a variable that has one nonmissing numeric value per group, and all the other values are missing per group. I want to replace the missing values with the nonmissing value per group. The place where the nonmissing value appears changes.
For instance:
I tried the following code but it didn't completely work.
bysort cow: replace var1 = var1[_n-1] if missing(var1)
bysort cow: replace var1 = var1[_n+1] if missing(var1)
Any suggestions would be greatly appreciated. Thank you.
I have a variable that has one nonmissing numeric value per group, and all the other values are missing per group. I want to replace the missing values with the nonmissing value per group. The place where the nonmissing value appears changes.
For instance:
country code | old var1 | new var1 (wanted) |
1 | . | 3 |
1 | . | 3 |
1 | 3 | 3 |
1 | . | 3 |
2 | . | 5 |
2 | 5 | 5 |
2 | . | 5 |
2 | . | 5 |
2 | . | 5 |
bysort cow: replace var1 = var1[_n-1] if missing(var1)
bysort cow: replace var1 = var1[_n+1] if missing(var1)
Any suggestions would be greatly appreciated. Thank you.
Comment