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

RE: st: filling previous missing observations repeatedly.


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: filling previous missing observations repeatedly.
Date   Mon, 12 Jul 2004 16:56:55 +0100

Your -while- syntax is not going to work the way 
you want if only because -while- is not going to loop 
over observations unless you tell it to. There are
other reasons why it would not work, but let's set
those aside. 

In some circumstances when you want to work backwards, 
you are best off creating a variable which is MINUS the year 
and sorting on that first. 

In this case, the time order doesn't appear central 
to what you're doing, so a solution looks like 

su B if country == "Belgium", meanonly  
replace B = `r(mean)' * C if mi(B) & Country == "Belgium" 

There is an FAQ on this subject also at 
http://www.stata.com/support/faqs/data/missing.html

Nick 
[email protected] 

Dev Vencappa

> Stata users, I have the following data set
> 
> year           B           C          Country
> 1995          .            x           Belgium
> 1996          .            x           Belgium
> 1997          .            x           Belgium
> 1998          .            x           Belgium
> 1999          x           x           Belgium
> 2000          .            x           Belgium
> 2001          .            x           Belgium
> 2002          .            x           Belgium
> 
> Variable B has data for only 1999 and missing for other 
> years. I want to extrapolate missing years using variable C, 
> which is some kind of multiplicative scalar that varies over 
> the years. Writing a command like the following seems to work 
> fine to fill missing values after year 1999:
> 
> replace B=B[_n-1]*C if B==. & year>1999 & country=="Belgium"
> 
> However, the following does not seem to work (or at least 
> fills the missing value for the year 1998 only):
>  
> replace B=B[_n+1]*C if B==. & year<1999
> 
> I believe this is because Stata works only forward and not 
> backward. Is there a way to ask Stata to work backwards 
> starting with the value from the year 1999 and repeat the 
> command for prior years? I have tried the following loop 
> command but that does not seem to work either (this command 
> does not even fill in the missing value for the year 1998, 
> for obvious reasons)
> 
> while B[_n+1]!=. & B==. & country=="Belgium"{
> replace B=B[_n+1]*C
> }
> 
> I think I am missing a syntax of Stata here.

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