Dear Statalists,
I have the following
clear
input byte(id x)
1 .
1 18
1 12
1 20
2 .
2 .
2 15
2 22
end
And I would like to generate a variable -- call it frstnmx --- that takes the first non-missing value of x by id:
clear
input byte(id x) float var3
1 . 18
1 18 18
1 12 18
1 20 18
2 . 15
2 . 15
2 15 15
2 22 15
end
I found several solutions to similar questions in the archive.
The solution from Scott Merryman (http://www.stata.com/statalist/archi.../msg00509.html)
--
by id: egen frstnmx=min(x)
--
would not work as the first non-missing value is not necessarily the smallest one.
The solution from Sebastian F. Buechte (http://www.stata.com/statalist/archi.../msg00090.html)
---
gen x=.;
by id: replace x=cond(var3[_n]==.,var3[_n+1],.);
---
works with id==1 but not id==2.
I also tried solutions from Nick Cox and David Airey (http://www.stata.com/statalist/archi.../msg00540.html)
---
by id: gen obs = _n
by id: summarize obs if !missing(x), meanonly
by id: replace x = x[r(min)] if _n < r(min)
---
but it did not work (no changes made to the data).
I found a discussion from Nick Cox and Eric A. Booth on generating variable taking first non-zero value in a row (http://www.stata.com/statalist/archi.../msg00953.html) useful; but still I wasn't able to come up with a solution to my question.
Please help; and many thanks!
Lucy
I have the following
clear
input byte(id x)
1 .
1 18
1 12
1 20
2 .
2 .
2 15
2 22
end
And I would like to generate a variable -- call it frstnmx --- that takes the first non-missing value of x by id:
clear
input byte(id x) float var3
1 . 18
1 18 18
1 12 18
1 20 18
2 . 15
2 . 15
2 15 15
2 22 15
end
I found several solutions to similar questions in the archive.
The solution from Scott Merryman (http://www.stata.com/statalist/archi.../msg00509.html)
--
by id: egen frstnmx=min(x)
--
would not work as the first non-missing value is not necessarily the smallest one.
The solution from Sebastian F. Buechte (http://www.stata.com/statalist/archi.../msg00090.html)
---
gen x=.;
by id: replace x=cond(var3[_n]==.,var3[_n+1],.);
---
works with id==1 but not id==2.
I also tried solutions from Nick Cox and David Airey (http://www.stata.com/statalist/archi.../msg00540.html)
---
by id: gen obs = _n
by id: summarize obs if !missing(x), meanonly
by id: replace x = x[r(min)] if _n < r(min)
---
but it did not work (no changes made to the data).
I found a discussion from Nick Cox and Eric A. Booth on generating variable taking first non-zero value in a row (http://www.stata.com/statalist/archi.../msg00953.html) useful; but still I wasn't able to come up with a solution to my question.
Please help; and many thanks!
Lucy
Comment