Can anyone advise about how to replace missing values with the next value to the right of the same type. I've been attempting using replace but because this actually runs to x and y 150 it's proving quite laborious as some of the missings run for 100 or more consecutively. Maybe I should add that x1...xn and y1....yn are related to each other which is causing me problems if I were to reshape.


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(id x1 x2 x3 x4 y1 y2 y3 y4)
 1 31  .  .  . 1 . . .
 2 21  . 16  . 1 . 2 .
 3 22  4  .  . 1 1 . .
 4  3  . 22  . 2 . 2 .
 5 44  .  .  . 2 . . .
 6  5  3  .  . 2 1 . .
 7 16  .  .  . 1 . . .
 8 18  4  . 99 2 2 . 1
 9 12  .  .  . 2 . . .
10 21 11  2  . 1 2 2 .
end
I'm wanting it to look like this

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(id x1 x2 x3 x4 y1 y2 y3 y4)
 1 31  .  . . 1 . . .
 2 21 16  . . 1 2 . .
 3 22  4  . . 1 1 . .
 4  3 22  . . 2 2 . .
 5 44  .  . . 2 . . .
 6  5  3  . . 2 1 . .
 7 16  .  . . 1 . . .
 8 18  4 99 . 2 2 1 .
 9 12  .  . . 2 . . .
10 21 11  2 . 1 2 2 .
end