Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[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" <[email protected]> 
To 
 
"[email protected]" <[email protected]> 
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/