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

st: RE: while loops [was: filling previous missing observations repeatedly]


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: while loops [was: filling previous missing observations repeatedly]
Date   Mon, 12 Jul 2004 19:04:37 +0100

We're both right on this. And I'm wrong. 

In essence, I was too quick on the draw
and shot you by mistake. Sorry. Every 
other time I've seen this code use 
it's been based on a misunderstanding. 

As I emphasised, 

while b == . { 
	... 
} 

is equivalent to 

while b[1] == . { 
	...
} 

and if that's not understood, problems 
almost always ensue. But in your example, 
b[1] starts out as missing and the loop continues 
until b[1] is no longer missing. 
This gives exactly what you want, at the 
cost of some inefficiency. However, I wonder
whether there are examples in which you 
would exit prematurely.  

Nick 
[email protected] 

> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]]On Behalf Of
> [email protected]
> Sent: 12 July 2004 18:39
> To: [email protected]
> Subject: st: while loops [was: filling previous missing observations
> repeatedly]
> 
> 
> Nick,
> 
> I guess I do not understand the problem with -while- (and, 
> why for Dev it only worked for 1998).  Maybe I have 
> misunderstood the initial problem.
> 
> 
> Here is what I did:
> 
> **Assume the initial data set is as follows:
> 
> . l
> 
>      +--------------+
>      | year   c   b |
>      |--------------|
>   1. | 1995   1   . |
>   2. | 1996   2   . |
>   3. | 1997   3   . |
>   4. | 1998   4   . |
>   5. | 1999   5   5 |
>      |--------------|
>   6. | 2000   6   . |
>   7. | 2001   7   . |
>   8. | 2002   8   . |
>      +--------------+
> 
> **Run the -while- loop
> 
> . do "C:\DOCUME~1\SCOTT~1.MER\LOCALS~1\Temp\STD00000000.tmp"
> 
> .  while b == . {
>   2.         replace b=b[_n+1]*c if b==. & year <1999
>   3.         replace b=b[_n-1]*c if b==. & year >1999
>   4.  }
> (1 real change made)
> (3 real changes made)
> (1 real change made)
> (0 real changes made)
> (1 real change made)
> (0 real changes made)
> (1 real change made)
> (0 real changes made)
> 
> . 
> end of do-file
> 
> . l
> 
>      +-----------------+
>      | year   c      b |
>      |-----------------|
>   1. | 1995   1    120 |
>   2. | 1996   2    120 |
>   3. | 1997   3     60 |
>   4. | 1998   4     20 |
>   5. | 1999   5      5 |
>      |-----------------|
>   6. | 2000   6     30 |
>   7. | 2001   7    210 |
>   8. | 2002   8   1680 |
>      +-----------------+
> 
> 
> Scott
> 
> ----- Original Message -----
> From: Nick Cox <[email protected]>
> Date: Monday, July 12, 2004 12:07 pm
> Subject: RE: st: filling previous missing observations repeatedly.
> 
> > This is not going to work for reasons I gave 
> > and for reasons I alluded to in an earlier 
> > posting. 
> > 
> > -while- does not buy you a loop over observations 
> > unless you ask for that explicitly. 
> > 
> > Also, Stata is going to interpret this as 
> > 
> > while b[1] == . { 
> > 	...
> > }
> > 
> > so, depending on b[1], the loop is either never
> > entered or infinite. 
> > 
> > Nick 
> > [email protected] 
> > 
> 
> 
> *
> *   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/
> 

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