clear
set more off

********************************************************************************
********************************************************************************
*	Import 2020 election data
********************************************************************************
********************************************************************************

* Municipalities with more than 1000 inhabitants
********************************************************************************

import excel "$source\elections\2020-03-16-resultats-communes-de-1-000-et-plus.xlsx", sheet("2020-03-16 résultats communes d") firstrow clear allstring

duplicates report Codedudépartement Codedelacommune
assert r(unique_value) - r(N) == 0 // Check absence of duplicates

renvars Codedudépartement Libellédudépartement Codedelacommune Inscrits Votants \ departement_code departement_name commune_code voters votes
keep departement_code departement_name commune_code voters votes

tempfile tmp1
save `tmp1', replace

* Municipalities with less than 1000 inhabitants
********************************************************************************

import excel "$source\elections\2020-03-16-resultats-communes-de-moins-de-1-000.xlsx", sheet("2020-03-16 résultats communes d") firstrow clear allstring

duplicates report Codedudépartement Codedelacommune
assert r(unique_value) - r(N) == 0 // Check absence of duplicates

renvars Codedudépartement Libellédudépartement Codedelacommune Inscrits Votants \ departement_code departement_name commune_code voters votes
keep departement_code departement_name commune_code voters votes

* Append both groups of municipalities
********************************************************************************

append using `tmp1'

duplicates report departement_code commune_code
assert r(unique_value) - r(N) == 0 // Check absence of duplicates after apppend

* Sum voters and votes by department
********************************************************************************

destring voters votes, replace

collapse (sum) voters votes, by(departement_code departement_name)

* Compute turnout at the department level
********************************************************************************

gen turnout = votes/voters

summ turnout
assert inrange(r(min),0,1)
assert inrange(r(max),0,1)

* Harmonize department identifier
********************************************************************************

replace departement_code = "0" + departement_code if length(departement_code) == 1

replace departement_code = "971" if departement_code == "ZA"
replace departement_code = "972" if departement_code == "ZB"
replace departement_code = "973" if departement_code == "ZC"
replace departement_code = "974" if departement_code == "ZD"
replace departement_code = "976" if departement_code == "ZM"
replace departement_code = "988" if departement_code == "ZN"
replace departement_code = "975" if departement_code == "ZS"

* Save
********************************************************************************
compress
save "$temp/departement_turnout_2020", replace
