* need to specify proper directory path in the fourth line

set more off
local directory .../replicationfiles/data_generation/datasources/WHO_mortality_database_WHOSIS/
cd `directory'
local datasets " morticd07 morticd08 morticd09 morticd10 "
capture: mkdir tempdump
cd tempdump

foreach dataset in `datasets' {

	copy http://www.who.int/healthinfo/statistics/`dataset'.zip `dataset'.zip
	unzipfile `dataset'.zip
	erase `dataset'.zip
	local WHOname : dir . files "*"
	gettoken wtf : WHOname
	insheet using "`wtf'.", comma
	erase "`wtf'."
	keep country year list cause deaths1 admin1 subdiv
	drop if mi(admin1)==0 | mi(subdiv)==0 // if these variables exist, it means data was collected only for part of the country
	drop admin1 subdiv
	collapse (sum) deaths1, by(country year list cause) // this adds male, female, and "unspecified" deaths
	if "`dataset'"=="morticd07" keep if cause=="A149"
	if "`dataset'"=="morticd08" keep if cause=="A148" | cause=="B050" // A148 includes “legal intervention," B050 includes war
	if "`dataset'"=="morticd09" keep if cause=="B55"
	if "`dataset'"=="morticd10" {
		forvalues n=85/99 {
			replace cause="1102" if cause=="X`n'"
			}
		forvalues n=850/999 {
			replace cause="1102" if cause=="X`n'"
			}
		forvalues n=0/9 {
			replace cause="1102" if cause=="Y0`n'" | cause=="Y00`n'"
			}
		forvalues n=10/99 {
			replace cause="1102" if cause=="Y0`n'"
			}
		replace cause="1102" if cause=="UE64" // special code for Portugal
		keep if cause =="1102"
		}

	drop cause
	collapse (sum) homicides=deaths1, by(country year list)
	save `dataset'.dta, replace
	clear
	}

foreach dataset in `datasets' {
	append using `dataset'
	erase `dataset'
	}	
	
cd `directory'
capture: rmdir tempdump

merge m:1 country using WHO_countrycodes, keepusing(ISO3code) keep(1 3) nogenerate // cf. notes_on_cleaning.docx
drop country
drop if mi(ISO3code)==1 // this concerns 4 countries or provinces, cf. notes
drop if ISO3code=="ENG" | ISO3code=="SCO" | ISO3code=="UKI" // UK is in the data as an aggregate
order ISO3code year
label variable homicides "homicides (total) from WHO-SIS (various ICD versions)"
rename list ICDversion
label variable ICDversion "version of ICD under which homicides was compiled"
* save WHOSIS_homicides, replace
