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.
0 Response to How to fill in missing values by group when there is one nonmissing value per group
Post a Comment