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

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


From   [email protected]
To   [email protected]
Subject   st: while loops [was: filling previous missing observations repeatedly]
Date   Mon, 12 Jul 2004 12:38:55 -0500

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/



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