replacing missing values for zeros in a loop

#1
Hello Stata experts,
I am working in panel data with dyadic data and years. My data base is like the following:
pair year fdi
1 2005 .
1 2006 .
1 2007 .
1 2008 .
1 2009 0
1 2010 0
1 2011 0
2 2005 .
2 2006 .
2 2007 .
2 2008 0
2 2009 0
2 2010 .
2 2011 .
3 2005 .
3 2006 .
3 2007 .
3 2008 2
3 2009 4
3 2010 .
3 2011 .

I want to replace the missing values of the variable fdi for 0s following 2 conditions (both have to be true):
1. the change will be applicable only if for a pair during the whole sample period there are just missing values and 0s.
2. the missing value corresponds to periods before the first 0 in a pair (missing values after the first 0 are not changed)

So, in my example the missing values that would be replaced by 0 are a)in pair 1 in years 2005-2008. b)in pair 2 in years 2005-2007.
I tried with this code :
bysort pair (year): egen numz=mean(fdi) //if the mean is 0 the condition 2 holds
while inwardfdi==.{
bysort pair year: replace inwardfdi=0 if numz==0
}
But every missing value from a pair that holds condition 1 gets changed to 0 this way. Can somebody help me?
 
Last edited:

maartenbuis

TS Contributor
#2
There is no need for looping:

Code:
clear

// some example data
input ///
pair year fdi
1 2005 .
1 2006 .
1 2007 .
1 2008 .
1 2009 0
1 2010 0
1 2011 0
2 2005 .
2 2006 .
2 2007 .
2 2008 0
2 2009 0
2 2010 .
2 2011 .
3 2005 .
3 2006 .
3 2007 .
3 2008 2
3 2009 4
3 2010 .
3 2011 .
end

// first condition 
gen byte zeromiss = inlist(fdi,0,.)
bys pair (year) : gen onlyzeromiss = sum(zeromiss)
bys pair (year) : replace onlyzeromiss = (onlyzeromiss[_N]==_N)

// second condition
gen byte zero = (fdi == 0)
bys pair (year): gen runsumzero = sum(zero)

// the replace
replace fdi = 0 if fdi ==. & onlyzeromiss == 1 & runsumzero == 0 

// admire the result
list, sepby(pair)