Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

re: st: How can I replace missing values with the variable's first nonmissing value?


From   "Airey, David C" <david.airey@vanderbilt.edu>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   re: st: How can I replace missing values with the variable's first nonmissing value?
Date   Sat, 11 Aug 2012 11:08:24 -0500

.

Here is a try:

clear
set obs 100
gen x = runiform()
gen y = runiform()
replace x = . if y > 0.8 // 20% missing
drop y
gen notmissing = _n if !missing(x)
egen r1 = min(notmissing)
gen r2 = x[r1]
gen x2 = x
replace x2 = r2 if missing(x)
drop notmissing r1 r2
browse



> Dear all,
> 
> I want to replace a variable's missing value with its first nonmissing
> value, how can I realize this? I read the FAQ at
> 
> http://www.stata.com/support/faqs/data-management/replacing-missing-values/
> ,
> but there is no example for my case.
> 
> My data structure is like :
> 
> _n  myvar
> 1    .
> 2    .
> 3    .
> 4    1
> 5    2
> 6    6
> 7    .
> 
> After replacement, I want to get data as:
> 
> _n  myvar
> 1    1
> 2    1
> 3    1
> 4    1
> 5    2
> 6    6
> 7    1
> 
> Thank you.
> 
> Best,
> Sharon
> 



*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index