| |
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: loop to fill in missing observations
Dear Statalist,
I have a dataset of national elections in 28 countries. Observations
are elections. This means that there can be several elections in the
same year, and on the other hand only years when an election was held
are included in the dataset.
I want to fill in missing years for each country. My idea was to loop
over countries and years to check for every country if a given year
is present, and if not fill it in. To do so, I have created an
appropriate number of missing observations to be filled in, and a
counter variable to identify them.
Concretely, the dataset looks like this:
countryn year elecn fillin
1 1990 1 .
1 1994 2 .
1 1994 3 .
1 1997 4 .
2 1989 1 .
2 1992 2 .
2 1995 3 .
2 1999 4 .
2 2000 5 .
. . . 1
. . . 2
. . . 3
And my code is the following:
g n=.
local z=1
qui forval x=1/28 {
forval y=1975/2002 {
sum year if year==`y' & countryn==`x'
replace n=r(N)
replace year=`y' if n==0 & fillin==`z'
replace countryn=`x' if n==0 & fillin==`z'
count if countryn==`x' & year==`y' & fillin==`z'
local w=r(N)
local z=`z'+`w'
}
}
Now, for some countries it works fine, but for most some missing
years are not filled in. It does not seem to depend on whether in the
country there was more than one election in some year, and I cannot
find any pattern that could help me identify the problem.
What am I doing wrong?
Many thanks in advance,
Fabrizio
------------------------------------------------------------
Fabrizio Gilardi, PhD
Institut d'Etudes Politiques et Internationales
Universit� de Lausanne
B�timent Anthropole
CH-1015 Lausanne
Switzerland
Phone +41 21 692 31 48
Fax + 41 21 692 31 45
[email protected]
http://wwwpeople.unil.ch/fabrizio.gilardi/
------------------------------------------------------------
*
* 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/