clear
set more off

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

* Select file to import
***********************

local file = "sursaud-covid19-quotidien-2020-06-20-19h00-departement" // June 20 download

* Import
********
import delimited "$source/covid_epidemics/`file'.csv", delimiter(comma) varnames(1) clear

* Keep only the "total" age category
************************************
keep if sursaud_cl_age_corona == "0"
drop sursaud_cl_age_corona

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

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

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

* 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

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