* 4_Balance_Test_.do: Generate balance tests on matached sample. 

clear all

set more off

capture log close
log using "4_Balance_Test_log.txt", text replace


use "3_Analysis.dta", clear
	local controls = "LogSaleqL1_mz DOI MBR ppentq_qpc"
    drop if k2k != 1

**** Weighted means tests for treated and controls in many-to-many matched group
	svyset [pweight=cem_weights]
	display "Many-to-many matched group"
	display "variable,Control N,Control mean,Control SE,Treat N,Treat mean,Treat SE,std diff in means,p-value"
    quietly regress sd_cogsDiff8 i.treatnow8 `ctrls' `macroctrls' i.year i.sic2, vce(cluster cusip)
	foreach var in sdcogs8T_1 `controls' {
		quietly _pctile `var' if treatnow8 == 0 & e(sample), p(50)
		local median0 = `r(r1)'
		quietly _pctile `var' if treatnow8 == 1 & e(sample), p(50)
		local median1 = `r(r1)'
        quietly ttest `var' if e(sample), by(treatnow8)
        local mean0 = `r(mu_1)'
        local mean1 = `r(mu_2)'
        local SE0 = `r(sd_1)'/sqrt(`r(N_1)')
        local SE1 = `r(sd_2)'/sqrt(`r(N_2)')
        local N0 = `r(N_1)'
        local N1 = `r(N_2)'
		local p=`r(p)'
		local stddifmean = (`r(mu_1)'- `r(mu_2)') /  (`r(sd)')
		display %25s "`var',",%5.0f `N0',",",%9.3f `mean0',",",%9.3f `SE0',",",%5.0f  `N1',",",%9.3f `mean1',",",%9.3f `SE1',",",%6.3f `stddifmean',",",%4.2f `p'
		}

use "3_Analysis_drop.dta", clear
	local controls = "LogSaleqL1_mz DOI MBR ppentq_qpc"
    drop if k2k != 1
    
**** Weighted means tests for treated and controls in many-to-many matched group
	svyset [pweight=cem_weights]
	display "Many-to-many matched group"
	display "variable,Control N,Control mean,Control SE,Treat N,Treat mean,Treat SE,std diff in means,p-value"
    regress sd_cogsDiff8drop i.treatnow8 `ctrls' `macroctrls' i.year i.sic2, vce(cluster cusip)
	foreach var in sdcogs8T_1 `controls' {
		quietly _pctile `var' if treatnow8 == 0 & e(sample), p(50)
		local median0 = `r(r1)'
		quietly _pctile `var' if treatnow8 == 1 & e(sample), p(50)
		local median1 = `r(r1)'
        quietly ttest `var' if e(sample), by(treatnow8)
        local mean0 = `r(mu_1)'
        local mean1 = `r(mu_2)'
        local SE0 = `r(sd_1)'/sqrt(`r(N_1)')
        local SE1 = `r(sd_2)'/sqrt(`r(N_2)')
        local N0 = `r(N_1)'
        local N1 = `r(N_2)'
		local p=`r(p)'
		local stddifmean = (`r(mu_1)'- `r(mu_2)') /  (`r(sd)')
		display %25s "`var',",%5.0f `N0',",",%9.3f `mean0',",",%9.3f `SE0',",",%5.0f  `N1',",",%9.3f `mean1',",",%9.3f `SE1',",",%6.3f `stddifmean',",",%4.2f `p'
		}

use "3_Analysis_as.dta", clear
	local controls = "LogSaleqL1_mz DOI MBR ppentq_qpc"
    drop if k2k != 1
    
**** Weighted means tests for treated and controls in many-to-many matched group
	svyset [pweight=cem_weights]
	display "Many-to-many matched group"
	display "variable,Control N,Control mean,Control SE,Treat N,Treat mean,Treat SE,std diff in means,p-value"
    quietly regress sd_cogsDiff8as i.treatnow8 `ctrls' `macroctrls' i.year i.sic2, vce(cluster cusip)
	foreach var in sdcogs8T_1 `controls' {
		quietly _pctile `var' if treatnow8 == 0 & e(sample), p(50)
		local median0 = `r(r1)'
		quietly _pctile `var' if treatnow8 == 1 & e(sample), p(50)
		local median1 = `r(r1)'
        quietly ttest `var' if e(sample), by(treatnow8)
        local mean0 = `r(mu_1)'
        local mean1 = `r(mu_2)'
        local SE0 = `r(sd_1)'/sqrt(`r(N_1)')
        local SE1 = `r(sd_2)'/sqrt(`r(N_2)')
        local N0 = `r(N_1)'
        local N1 = `r(N_2)'
		local p=`r(p)'
		local stddifmean = (`r(mu_1)'- `r(mu_2)') /  (`r(sd)')
		display %25s "`var',",%5.0f `N0',",",%9.3f `mean0',",",%9.3f `SE0',",",%5.0f  `N1',",",%9.3f `mean1',",",%9.3f `SE1',",",%6.3f `stddifmean',",",%4.2f `p'
		}

capture log close
