Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: A question concerning Stata logic


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: A question concerning Stata logic
Date   Thu, 1 Dec 2005 12:47:14 -0000

Consider 

. replace var3=var3[_n+1] if var3==. & _n<_N

The restriction _n < _N only bites with the last 
value. In this example, it makes no difference,  
as a reference to var3[_N+1] is evaluated as missing, 
and so the restriction can be set on one side. 

In words, you are asking Stata to replace missing with 
the next value. Only in the case of var3[2] 
does this make any difference, but Stata is 
doing exactly what you asked. Perhaps the 
principle being missed is given by the 
title of a note by Roger Newson, one that 
is also stressed in various FAQs, including
the one cited below. 

SJ-4-4  dm0008   Stata tip 13: generate and replace use the current sort order
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  R. Newson
        Q4/04   SJ 4(4):484--485                                 (no commands)
        tips for using generate and replace, which use the
        current sort order

In addition what I think is your problem is discussed 
at length in 

FAQ     . . . . . . . . . . . . . . . . . . . . . . . Replacing missing values
        2/03    How can I replace missing values with previous or
                following nonmissing values?
                http://www.stata.com/support/faqs/data/missing.html

Nick 
[email protected] 

Daniel Hoechle
 
> 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?

*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index