I would like to fill up values for a variable, say number, with the first (and only) non-missing number in the same group (captured by the group identifier id) such that
will be completed to
I do realize that this question has been asked and answered before. I have found some useful initial help here and here. I ended up with three different versions of code:
However, neither of these versions would produce the result that I am looking for (see table above). For some ids (say id=1 and id=3) it worked, for others it did not (I got only missing number values for id=2 despite the fact that the number was nonmissing for id=2 in the original data).
Do you have some other suggestions I could try? What could be possible potential sources of error? What additional information could I look for / provide to make the error hunting feasible?
Many thanks,
Milan
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input byte id double number 1 23 1 . 1 . 2 12 2 . 3 5 3 . 3 . end
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input byte id double number 1 23 1 23 1 23 2 12 2 12 3 5 3 5 3 5 end
Code:
bysort id : replace number = number[_n-1] if missing(number) & _n > 1 bysort id : replace number = number[1] bysort id (number): replace number=number[_N]
Do you have some other suggestions I could try? What could be possible potential sources of error? What additional information could I look for / provide to make the error hunting feasible?
Many thanks,
Milan
Comment