clear
set more off

set seed 2877 // Random integer between 0 and 3000 from https://www.random.org
set sortseed 936 // Random integer between 0 and 3000 from https://www.random.org

* Load "find_synth" program
do "$do\11_0_SCM_program_define.do"

foreach dep in abret comply dist{

use "$temp/final_data", clear

* Select dependent variable
if "`dep'" == "abret"{
local var an_abret_index_SBF120_30_1
keep  if  type == "Purchase"
}
if "`dep'" == "comply"{
local var diff_comply
}
if "`dep'" == "dist"{
local var dist_next_res_all
}

find_synth `var' after connect id_ind time 5000 10

expand dup

* Difference in each sub-period
forvalues k = 1(1)8{
ttest `var' if time == `k', by(connect)
local diff_`k'_`dep' =  r(mu_2) -  r(mu_1)
}

* Difference in differences, raw
ttest `var' if after == 0, by(connect)
local D0_`dep' = r(mu_2) -  r(mu_1)
ttest `var' if after == 1, by(connect)
local D1_`dep' = r(mu_2) -  r(mu_1)
local DD_`dep' = `D1_`dep'' - `D0_`dep''

* Difference in difference, regression form
reg `var' connect inter after, ro
local inter_`dep' = _b[inter]
local connect_`dep' = _b[connect]
local after_`dep' = _b[after]
}

clear
set obs 3
foreach x in inter connect after D0 D1 DD{
gen `x' = .
}
forvalues k = 1(1)8{
gen diff_`k' = .
}
gen dep = ""

local a = 0
foreach dep in abret comply dist{
local a = `a' + 1
replace dep = "`dep'" if _n == `a'
foreach x in inter connect after D0 D1 DD{
replace `x' = ``x'_`dep'' if _n == `a'
}
forvalues k = 1(1)8{
replace diff_`k' = `diff_`k'_`dep'' if _n == `a'
}
}

save "$temp/scm_dependent_variables", replace
