clear
set more off

********************************************************************************
********************************************************************************
* Import COVID-19 epidemics data
********************************************************************************
********************************************************************************

* Import
********

local file = "sursaud-corona-quot-dep-2021-01-14-19h20" // January 14, 2021 download
import delimited "$source/covid/`file'.csv", delimiter(";") varnames(1) clear


* Keep only the "total" age category
************************************
egen cl_age = group(sursaud_cl_age_corona)
label define cl_age_label 1 "All ages", replace
label define cl_age_label 2 "Below 15", add
label define cl_age_label 3 "15-44", add
label define cl_age_label 4 "45-64", add
label define cl_age_label 5 "65-74", add
label define cl_age_label 6 "75+", add
label values cl_age cl_age_label
label var cl_age "Age class"

drop sursaud_cl_age_corona

keep if cl_age == 1


* Drop male- and female-specific variables
******************************************
drop *_h *_f

*  Clean date
*************
*gen date = date(date_de_passage, "DM20Y")
gen date = date(date_de_passage, "YMD")

drop date_de_passage
format date %td

* Missing data imputation
*************************
replace nbre_hospit_corona = 0 if dep == "48" & date == mdy(03,29,2020) & nbre_hospit_corona == .
* Series is 0 - 0 - missing - 1 - 0 : Let's replace by 0
replace nbre_pass_corona = 21 if dep == "48" & date == mdy(03,29,2020) & nbre_pass_corona == .
* Series is 24 - 16 - missing - 30 - 15 : Let's replace by 21

drop if date > mdy(09,1,2020)

* Compute running sum for pure flow variables
*********************************************
egen id = group(dep)
tsset id date

foreach var of varlist *{
if ~inlist("`var'","dep","date","id"){
	egen min_date_dep = min(date) if `var' != ., by(id)
	gen `var'_cum = `var' if date == min_date_dep
	replace `var'_cum = L.`var'_cum + `var' if date > min_date_dep
	drop min_date_dep
	}
}
drop id

* Change departement coding for merge with departement level variables
**********************************************************************
foreach w in 1 2 3 4 5 6 7 8 9 {
	replace dep = "0`w'" if dep == "`w'"
}

* Rename and label variables
****************************
rename dep departement_code

label var nbre_hospit_corona "Number of hospitalizations for COVID-19 suspicion"

* Keeping only relevant variables
*********************************
keep departement_code nbre_hospit_corona date nbre_hospit_corona_cum

* Keep metropolitan France only
*******************************
keep if length(departement_code) == 2

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