* 2A_drop_CEM.do: 4 QTR ANALYSIS USING CUSTOMER DROP TREATMENT. Use coarsened exact matching to match treated firms (firms with a customer acquisition) to control firms (firms with no customer acquisition during the study period).

* Requirements for matching:
    * Firms must either be treated or controls, never both
    * A firm must be matched with another firm in the same period

clear
capture log close

log using 2A_drop_CEM_log.txt, text replace
set more off

*** Create set of observations based on all companies in COMPUSTAT. 
	use "1_Variable_Prep.dta", clear
    gen naics2 = substr(naics,1,2)
    destring naics2, replace
    keep if (naics2>=30 & naics2<=49)
    gen naics3 = substr(naics,1,3)
    destring naics3, replace
    drop if sd_cogsDiff4drop == .
    count
            
*** Check the balance for matching variables and non-matching control variables of interest
	* Create the binary treatment variable 
        gen byte treatnow4 = cond(treated4drop == 1, 1, 0)
        bysort cusip (year_qtr): egen treatever = max(treatnow4)
		local treatment "treatnow4"
        label variable treatnow4 "Treated"

*** Prepare for matching
    * For firms that are ever treated, keep only the periods which have a new customer in order to force matching on those observations (and preclude non-treated periods for treated firms from being used as controls)
			drop if treatever == 1 & treatnow4 == 0
            tab fyearq treatnow4
            
        * Identify how many treatments are missing matching variables
            nmissing sic2 year_qtr sdcogs4T_1 if treatnow4 == 1
            count if (sic2 == . | year_qtr == . | sdcogs4T_1 == .) & treatnow4 == 1
            assert `r(N)' <= 100
    
    * Bring in customer data
        capture drop datacqtr
        gen datacqtr = year_qtr
        format datacqtr %tq
        merge m:1 cusip datacqtr using "customer number\customer_number_event.dta", keep(master match) gen(_cust)
        mvencode c_number l_c_number, mv(0)
        tab treatnow4
        tab c_number if treatnow4 == 0

    * Remove variables other than those used in the match or needed to merge back the match results into the master data
        keep cusip year_qtr sdcogs4T_1 sic2 treatnow4 treatever vars_present_drop LogSaleqL1_mz c_number l_c_number gvkey_code sd1_cogs4drop sd2_cogs4drop
         
*** Run the CEM and check imbalance
	* The number following the "#" indicates the number of cutpoints for the preceding variable. "#0" means do not coarsen the variable at all
    * To set cut points for sdcogsT_1, I am using common percentiles generated by the summary command. 
        sum sdcogs4T_1 if treatnow4 == 1, det
                display `r(p10)' `" "' `r(p25)' `" "' `r(p50)' `" "' `r(p75)' `" "' `r(p90)'
        sum LogSaleqL1_mz if treatnow4 == 1, det
                display `r(p50)'
        
        count
        cem sic2 (#0) year_qtr (#0) sdcogs4T_1 (.05024342 .0846297 .14263131 .26746711 .47057402) LogSaleqL1_mz (4.349026) l_c_number (5.1 10.1) vars_present_drop (#0), treatment(`treatment') showbreaks
        * QQQ: Would rather not hardwire that values for sdcogsT_1 above, but the cem command seems to choke if the local variables are used. To see this potential problem,  run "display `r(p10)' `r(p25)'"

            tab cem_matched treatnow4 if cem_strata == .
            keep if cem_matched == 1 | treatnow4 == 1
		
    * Count observations in matched sample 
    	count if cem_matched == 1 &  treatnow4 == 1
    	count if cem_matched == 1 &  treatnow4 == 0
    	unique cusip if cem_matched == 1 & treatnow4 == 0

    * Create variable identifying one-to-one matching (k2k)
    	set seed 20091213
    	gen r = uniform()
        gen treatnever = 1 - treatever
    	sort cem_strata treatnow4 r 
    	bysort cem_strata treatnow4: gen seq = _n 
    	egen t_per_strata = sum(treatnow4), by(cem_strata)
    	egen c_per_strata = sum(treatnever), by(cem_strata)
    	egen min_of_tc = rowmin(t_per_strata c_per_strata)
    
    	gen k2k = seq <= min_of_tc if min_of_tc!=.	
        label variable k2k "Dummy variable identifying 1-to-1 matching"

    * Check whether there is a difference between treated and control firms
        ttest sdcogs4T_1 if cem_matched == 1, by(treatnow4)
        ttest sdcogs4T_1 if cem_matched == 1 & k2k == 1, by(treatnow4)
        
    * Save CEM results 
        ttest sd1_cogs4drop == sd2_cogs4drop if cem_matched == 1 & k2k == 1
        keep cusip year_qtr sdcogs4T_1 sic2 treatnow4 treatever cem_matched cem_matched cem_weights cem_strata k2k c_number l_c_number gvkey_code
        	keep if cem_matched==1
        duplicates drop
        unique gvkey_code year_qtr 
        assert `r(N)' == `r(unique)'

        compress
		save "2A_Matched_drop_DatasetNoDedup", replace

capture log close
