Generating a Country-Month Data Set Using Correlates of War State System Membership

How do you construct a country-month dataset for all countries for a set number of years? This is a bit tricky if you don’t want to spend too much time working in Excel and don’t have a consistent list of all countries active in the international system throughout time.

Fortunately, after some quick coding, you can produce pretty much any range of months in Stata.  The trick is to have a list of countries to start with. Here I use the Correlates of War State System list.

Here are the step by step instructions based on a country-year sample from January 2000 to December 2015.  In reality you could alter the code to fit any range of years.

(1) Import the State System data from the Correlates of War

import delimited "http://correlatesofwar.org/data-sets/state-system-membership/states2011/at_download/file", clear

(2) After importing into Stata, it is just a matter of telling Stata how to expand the data and then adjusting the observed year and month variables for each observation. First you want to drop all countries that leave the state system before your observed years.

drop if endyear<2000

(3) Then generate a year variable for 2000 or the year that the country is first observed; and a month variable for January or the first month the country is observed.

gen year = 2000 if styear<2000
replace year= styear if styear>=2000
gen month = 1 if year==2000
replace month = stmonth if year>2000

(4) Use the “egen concat” command to generate a temporary begin date variable by combining the month and year.

egen date = concat(month year)

(5) Then use Nicholas Cox’s “todate” command to transform the temporary date variable into a State recognized date format variable. You can get this command using the “findit” command in Stata.

todate date, gen(date2) p(myyyy)
drop date
rename date2 date

(6) Generate an end date variable for the last month of observation, in this case December 2015.

gen enddate = "122015"
todate enddate, gen(edate) p(mmyyyy)
drop enddate

(7) Then calculate the difference between the initial date variable and the end date variable.

gen diff = edate-date

(8) Use the Stata command “expand” to add additional observations for the country months you need beween the beginning date (e.g. Jan 2000) and end date (e.g. Dec 2015).

expand diff+1
drop diff

(9) Then generate a numerical time variable that starts at zero for the first month (e.g. Jan 2000) and counts up to the last month (e.g. Dec 2015).

bys ccode: gen time= _n-1

(10)  Use the time variable to generate the proper date variable for the observation.

replace date = date+time
gen date2 = dofm(date)
format date2 %d

(11)  And use that date variable to fill in the accurate month and year values.

replace month=month(date2)
replace year=year(date2)

(12) Clean up the data and save!

drop edate date2
save "cmonth0015.dta", replace

The full code is also below for easy copy and paste into Stata. Keep in mind if you want to have a different range of months/ years, just alter the years and months entered below.

import delimited "http://correlatesofwar.org/data-sets/state-system-membership/states2011/at_download/file", clear
drop if endyear<2000  
gen year = 2000 if styear<2000
replace year= styear if styear>=2000 
gen month = 1 if year==2000 
replace month = stmonth if year>=2000 
drop month
tab year
gen month = 01 if year==2000
replace month = stmonth if year>2000
egen date = concat(month year)
todate date, gen(date2) p(myyyy)
drop date
rename date2 date
gen enddate = "122015"
todate enddate, gen(edate) p(mmyyyy)
drop enddate
gen diff = edate-date
expand diff+1
drop diff
bys ccode: gen time= _n-1
replace date = date+time
gen date2 = dofm(date)
format date2 %d
replace month=month(date2)
replace year=year(date2)
drop edate date2
save "cmonth0015.dta", replace
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s