| |
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: Re: loop to fill in missing observations
Dear Fabrizio,
As you need four observations for each country and year, you can turn
the year and election to a quarter variable, that is, treat the first obs
for each year as spring and the second as summer. then you have two
variables to identify each election, that is, country and season.
then you can use the fillin command in stata.
suppose you have the following data, where X is the variable to be the same
with the previous observation
country season x
1 1 1
1 2 4
2 1 5
2 2 8
2 3 10
3 1 4
4 1 3
4 2 5
fillin country season
then u got the following data
country season x _fillin
1 1 1 0
1 2 4 0
1 3 . 1
2 1 5 0
2 2 8 0
2 3 10 0
3 1 4 0
3 2 . 1
3 3 . 1
4 1 3 0
4 2 5 0
4 3 . 1
is this what you need?
Cheers
Chuntao
----- Original Message -----
From: "Fabrizio Gilardi" <[email protected]>
To: <[email protected]>
Sent: Friday, June 15, 2007 10:25 PM
Subject: 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/
*
* 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/