Daniel Hoechle wrote:
Consider the following three identical variables to start with:
var1 var2 var3
. . .
. . .
. . .
2 2 2
3 3 3
4 4 4
. . .
. . .
. . .
Typing
. replace var2=var2[_n-1] if var2==. & _n>1
. replace var3=var3[_n+1] if var3==. & _n<_N
results in
var1 var2 var3
. . .
. . .
. . 2
2 2 2
3 3 3
4 4 4
. 4 .
. 4 .
. 4 .
Why does Stata replace all three missings in the first case but only the
first missing in the second case? Do I have to sort the dataset the
other way around in order to fill var3 in a similar way as var2 or is
there an easy workaround?
--------------------------------------------------------------------------------
I think that you might be mistaken about what Stata was replacing and not
replacing. I believe that Stata's logic would have it replacing *all* the
missings in rows 2 through end in the first case, and *all* of the missings
in rows 1 through N-1 in the second case. It's just that it replaced a
missing with another missing when the replacement, itself, was a missing
value. Compare the do-file below to see what I think that Stata was doing
in your dataset behind the scenes. (In truth, I'm not sure that Stata goes
through the effort of actually doing a replacement if it recognizes that the
replacement value is the same as the value to be replaced, regardless of
whether they're equal to missing.)
But to answer your question: as far as I know, yes; you'll need to reverse
the order of the observations or else run the -replace- repeatedly (see
below). Stata works down the list of observations--in your example, it
replaces observation 1 in var3 with another missing because that's what's
there in the current sort order, and likewise with observation 2. If you
want those replaced with something other than missing, then the next
observation down needs to be something other than missing.
Joseph Coveney
clear
set more off
input var1 var2 var3
. .a .a
. .b .b
. .c .c
2 2 2
3 3 3
4 4 4
. .d .d
. .e .e
. .f .f
end
replace var2=var2[_n-1] if missing(var2) & _n>1
replace var3=var3[_n+1] if missing(var3) & _n<_N
list, noobs separator(0)
// This following will avoid reverse-sorting, but
// might not be so efficient as reverse-sorting
// with a given dataset.
clear
set more off
input var1 var2 var3
. . .
. . .
. . .
2 2 2
3 3 3
4 4 4
. . .
. . .
. . .
end
while missing(var3[1]) {
replace var3=var3[_n+1] if missing(var3) & _n < _N
}
exit
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/