**** 3_Analysis.do - Conducts regressions for add treatments

capture log close
log using "3_Analysis_log.txt", text replace
set more off
clear all

capture erase 1_RegNoMatch_add_tex.txt
capture erase 1_RegNoMatch_add_tex.tex
capture erase 1_Parallel_add_tex.txt
capture erase 1_Parallel_add_tex.tex
capture erase 1_RegTrend_tex.txt
capture erase 1_RegTrend_tex.tex
capture erase 1_RegTrend_xml.txt
capture erase 1_RegTrend_xml.xml
program drop _all

	program outreg_Reg4_tex
        count if e(sample) & treated4 == 1
        local num_treat = `r(N)'
        unique cusip if e(sample)
        local num_firm = `r(unique)'
        local keep_var1 = "1.treatnow4 LogSaleqL1_mz c.LogSaleqL1_mz#c.LogSaleqL1_mz DOI MBR ppentq_qpc FAtoSales_T4L  CIqL1 c.CIqL1#c.CIqL1"
        outreg2 using 1_Reg4_tex, tex(fragment) addstat(Treated Observations, `num_treat') alpha(0.01, 0.05, 0.1) symbol(***, **, *) dec(4) label ctitle(`1') keep(`keep_var1') sortvar(`keep_var1') fmt(f)
    end

	program outreg_Reg8_tex
        count if e(sample) & treated8 == 1
        local num_treat = `r(N)'
        unique cusip if e(sample)
        local num_firm = `r(unique)'
        local keep_var1 = "1.treatnow8 LogSaleqL1_mz c.LogSaleqL1_mz#c.LogSaleqL1_mz DOI MBR ppentq_qpc FAtoSales_T4L  CIqL1 c.CIqL1#c.CIqL1"
        outreg2 using 1_Reg8_tex, tex(fragment) addstat(Treated Observations, `num_treat') alpha(0.01, 0.05, 0.1) symbol(***, **, *) dec(4) label ctitle(`1') keep(`keep_var1') sortvar(`keep_var1') fmt(f)
    end

	program outreg_Reg12_tex
        count if e(sample) & treated12 == 1
        local num_treat = `r(N)'
        unique cusip if e(sample)
        local num_firm = `r(unique)'
        local keep_var1 = "1.treatnow12 LogSaleqL1_mz c.LogSaleqL1_mz#c.LogSaleqL1_mz DOI MBR ppentq_qpc FAtoSales_T4L  CIqL1 c.CIqL1#c.CIqL1"
        outreg2 using 1_Reg12_tex, tex(fragment) addstat(Treated Observations, `num_treat') alpha(0.01, 0.05, 0.1) symbol(***, **, *) dec(4) label ctitle(`1') keep(`keep_var1') sortvar(`keep_var1')
    end

	program outreg_RegNoMatch_tex
        count if e(sample) & treatnow == 1
        local num_treat = `r(N)'
        unique cusip if e(sample) & treatnow == 1 
        local num_treat_firm = `r(unique)'
        unique cusip if e(sample) & treatnow == 0 
        local num_control_firm = `r(unique)'
        local keep_var1 = "1.treatnow LogSaleqL1_mz c.LogSaleqL1_mz#c.LogSaleqL1_mz DOI MBR ppentq_qpc FAtoSales_T4L  CIqL1 c.CIqL1#c.CIqL1"
        outreg2 using 1_RegNoMatch_add_tex, tex(fragment) addstat(Treated Observations, `num_treat', Treated Firms, `num_treat_firm', Control Firms, `num_control_firm') alpha(0.01, 0.05, 0.1) symbol(***, **, *) dec(4) label ctitle(`1') keep(`keep_var1') sortvar(`keep_var1') fmt(f)
    end

	program outreg_Parallel_tex
        unique cusip if e(sample)
        local num_firm = `r(unique)'
        local keep_var1 = "1.treatnowparallel#c.count3 1.treatnowparallel LogSaleqL1_mz c.LogSaleqL1_mz#c.LogSaleqL1_mz DOI MBR ppentq_qpc FAtoSales_T4L  CIqL1 c.CIqL1#c.CIqL1"
        outreg2 using 1_Parallel_add_tex, tex(fragment) alpha(0.01, 0.05, 0.1) symbol(***, **, *) dec(4) label ctitle(`1') keep(`keep_var1') sortvar(`keep_var1')
    end

    program outreg_RegTrend_tex
        args title estimate1 estimate2 estimate3 p1 p2 p3
        count if e(sample) & treatever == 1
        local num_treat = `r(N)'
        unique cusip if e(sample) & treatever == 1
        local num_firm = `r(unique)' 
        local keep_var1 = "1.treatever#1.tau0 1.treatever#1.tauP1 1.treatever#1.tauP2 1.treatever#1.tauP3 LogSaleqL1_mz c.LogSaleqL1_mz#c.LogSaleqL1_mz DOI MBR ppentq_qpc FAtoSales_T4L  CIqL1 c.CIqL1#c.CIqL1 1.tauP1 1.tauP2 1.tauP3"
        outreg2 using 1_RegTrend_tex, tex(fragment) addstat(Treated Observations, `num_treat', Treated Firms, `num_firm', Estimate1, `estimate1', pvalue1, `p1', Estimate2, `estimate2', pvalue2, `p2', Estimate3, `estimate3', pvalue3, `p3') alpha(0.01, 0.05, 0.1) symbol(***, **, *) dec(4) label ctitle(`title') keep(`keep_var1') sortvar(`keep_var1') fmt(f)
        outreg2 using 1_RegTrend_xml, excel addstat(Treated Observations, `num_treat', Treated Firms, `num_firm', Estimate1, `estimate1', pvalue1, `p1', Estimate2, `estimate2', pvalue2, `p2', Estimate3, `estimate3', pvalue3, `p3') alpha(0.01, 0.05, 0.1) symbol(***, **, *) dec(4) label ctitle(`title') keep(`keep_var1') sortvar(`keep_var1') fmt(f)
    end
    
capture erase 1_Reg4_tex.txt
capture erase 1_Reg4_tex.tex
**** Analysis - 4 quarter
**** Start with matched observations, merge in data with all variables to be used in regression analysis
    use "1_Variable_Prep.dta", clear
        keep if vars_present == 1

    merge 1:1 gvkey_code year_qtr using "2A_Matched_DatasetNoDedup.dta", keep(master match) gen(_CEM)
        tab treatnow _CEM

**** Model Free Check
    ttest sd1_cogs4 == sd2_cogs4 if cem_matched == 1 & treatnow4 == 1
    ttest sd_cogsDiff4 if cem_matched == 1 & k2k == 1, by(treatnow4)

    * There is no statistical difference in the pre-acquisition COGS variability between the treated4 and control firms
        ttest sdcogs4T_1 if cem_matched == 1 & k2k == 1, by(treatnow4)

    * Update treatever variable to identify firms that are treated
        drop treatever
        bysort cusip (year_qtr): egen treatever = max(treatnow4)
        tab treatnow4 treatever, missing
    
    * Identify all observations of a firm that was ever matched
        bysort cusip (year_qtr): egen cem_matched_ever = max(cem_matched)
        bysort cusip (year_qtr): egen k2k_ever = max(k2k)
        tab treatever cem_matched_ever, missing
        tab treatever k2k_ever, missing
            
    * Check validity of parallel trends assumption
        gen MatchPeriod = year_qtr if k2k == 1
        bysort cusip (year_qtr): egen MaxMatchPeriod = max(MatchPeriod)
        bysort cusip (year_qtr): egen MinMatchPeriod = min(MatchPeriod)
        format MatchPeriod MinMatchPeriod MaxMatchPeriod %tq
        gen count1 = year_qtr- MaxMatchPeriod
        gen count2 = year_qtr- MinMatchPeriod
        bysort cusip (year_qtr): egen MinCount = min(count2)
        gen count3 = count2 + abs(MinCount) + 1

        local ctrls = "c.LogSaleqL1_mz##c.LogSaleqL1_mz c.MBR##c.MBR c.DOI##c.DOI c.ppentq_qpc##c.ppentq_qpc c.FAtoSales_T4L##c.FAtoSales_T4L c.CIqL1##c.CIqL1"
        local macroctrls = ""
        bysort cusip (year_qtr): egen treatnowparallel = max(treatnow4)

        regress sdcogs4T_1 `ctrls' i.year_qtr `macroctrls' i.sic2 i.treatnowparallel##c.count3 if (count2 == -1 | count2 == -5 | count2 == -9 | count2 == -13) & cem_matched_ever == 1, vce(cluster cusip)
        outreg_Parallel_tex

    * Create data for table describing the observations ... copy the output into Excel and then save as a csv file to copy into the TEX file.
        tab year if treated4 == . & cem_matched == 1 & k2k == 1
        tab year if treated4 == 1 & cem_matched == 1 & k2k == 1
        tab sales_c if treated4 == . & cem_matched == 1 & k2k == 1
        tab sales_c if treated4 == 1 & cem_matched == 1 & k2k == 1
        tab sic2_c if treated4 == . & cem_matched == 1 & k2k == 1
        tab sic2_c if treated4 == 1 & cem_matched == 1 & k2k == 1

    * Identify control variables and moderators
        local ctrls = "c.LogSaleqL1_mz##c.LogSaleqL1_mz c.MBR##c.MBR c.DOI##c.DOI c.ppentq_qpc##c.ppentq_qpc c.FAtoSales_T4L##c.FAtoSales_T4L c.CIqL1##c.CIqL1"
        local macroctrls = ""

    * Run regressions
        regress sd_cogsDiff4 i.treatnow4 if vars_present == 1 & k2k == 1, vce(cluster cusip)
           outreg_Reg4_tex
    
        regress sd_cogsDiff4 i.treatnow4 `ctrls' if k2k == 1, vce(cluster cusip)
            outreg_Reg4_tex
            test LogSaleqL1_mz DOI MBR ppentq_qpc
    
        regress sd_cogsDiff4 i.treatnow4 `ctrls' i.year_qtr `macroctrls' i.sic2 if k2k == 1, vce(cluster cusip)
            outreg_Reg4_tex
            tab treatnow4 if e(sample)
            test LogSaleqL1_mz DOI MBR ppentq_qpc FAtoSales_T4L CIqL1
            testparm i.year_qtr
            testparm i.sic2      

    * Run anaysis without matched observations
        gen treatnow = treatnow4
        regress sd_cogsDiff4 i.treatnow `ctrls' i.year_qtr `macroctrls' i.sic2 if cem_matched == 1 [iweight=cem_weights], vce(cluster cusip)
            outreg_RegNoMatch_tex

    * Run time trend analysis
        bysort cusip (year_qtr): gen count = _n
        sort cusip year_qtr
        gen LastChange = .
        replace LastChange = cond(k2k==1, count, cond(cusip == cusip[_n-1], LastChange[_n-1], .))
        
        gsort cusip -year_qtr
        gen NextChange = .
        replace NextChange = cond(k2k==1, count, cond(cusip == cusip[_n-1], NextChange[_n-1], .))
        sort cusip year_qtr
        order cusip year_qtr count LastChange NextChange
        
        gen Set = cond(LastChange == ., NextChange, LastChange)
        gen RelativePeriod = count - Set
        order cusip year_qtr count LastChange NextChange Set RelativePeriod
        
        gen tau0 = cond(RelativePeriod == 0, 1, 0)
        gen tauP1 = cond(RelativePeriod == 4, 1, 0)
        gen tauP2 = cond(RelativePeriod == 8, 1, 0)
        gen tauP3 = cond(RelativePeriod == 12, 1, 0)
        bysort cusip (year_qtr): egen k2kAllPeriods = max(k2k)
        regress sd_cogsDiff4 1i.treatever#1i.tau0 1i.treatever#1i.tauP1 1i.treatever#1i.tauP2 1i.treatever#1i.tauP3 `ctrls' i.year_qtr i.sic2 if k2kAllPeriods == 1 & (tau0 == 1 | tauP1 == 1 | tauP2 == 1 | tauP3 == 1) , vce(cluster cusip)
            lincom 1.treatever#1.tau0 +1.treatever#1.tauP1
            local estimate1 = `r(estimate)'
            local p1 = (2 * ttail(`r(df)', abs(`r(estimate)'/`r(se)')))
            lincom 1.treatever#1.tau0 + 1.treatever#1.tauP1 + 1.treatever#1.tauP2
            local estimate2 = `r(estimate)'
            local p2 = (2 * ttail(`r(df)', abs(`r(estimate)'/`r(se)')))
            lincom 1.treatever#1.tau0 + 1.treatever#1.tauP1 + 1.treatever#1.tauP2 + 1.treatever#1.tauP3
            local estimate3 = `r(estimate)'
            local p3 = (2 * ttail(`r(df)', abs(`r(estimate)'/`r(se)')))
            outreg_RegTrend_tex Q4NoOverlap "`estimate1'" "`estimate2'" "`estimate3'" "`p1'" "`p2'" "`p3'"

**** Save 
    keep if vars_present == 1 & k2k == 1 & FAtoSales_T4L != .
    keep sd_cogsDiff4 treatnow4 LogSaleqL1_mz MBR DOI ppentq_qpc FAtoSales_T4L CIqL1 saleq_tot14L ppentq_avg14L atq_avg14L year_qtr sic2 k2k cem_matched cusip gvkey_code  vars_present
    compress
    save "3_Analysis4.dta", replace


capture erase 1_Reg8_tex.txt
capture erase 1_Reg8_tex.tex
**** Analysis - 8 quarter
**** Start with matched observations, merge in data with all variables to be used in regression analysis
    use "1_Variable_Prep.dta", clear
        keep if vars_present8 == 1

    merge 1:1 gvkey_code year_qtr using "2B_Matched_DatasetNoDedup.dta", keep(master match) gen(_CEM)
        tab treatnow _CEM

**** Model Free Check
    ttest sd1_cogs8 == sd2_cogs8 if cem_matched == 1 & treatnow8 == 1
    ttest sd_cogsDiff8 if cem_matched == 1 & k2k == 1, by(treatnow8)

    * There is no statistical difference in the pre-acquisition COGS variability between the treated8 and control firms
        ttest sdcogs8T_1 if cem_matched == 1 & k2k == 1, by(treatnow8)

    * Get value for demand variability in the pre-treatment period for the median firm. This is used to estimate the economic impact of the treatment (i.e. customer change event) on the median firm.
        sum sdcogs8T_1 if cem_matched == 1 & k2k == 1, det

    * Update treatever variable to identify firms that are treated
        drop treatever
        bysort cusip (year_qtr): egen treatever = max(treatnow8)
        tab treatnow8 treatever, missing
    
    * Identify all observations of a firm that was ever matched
        bysort cusip (year_qtr): egen cem_matched_ever = max(cem_matched)
        bysort cusip (year_qtr): egen k2k_ever = max(k2k)
        tab treatever cem_matched_ever, missing
        tab treatever k2k_ever, missing
            
    * Check validity of parallel trends assumption
        gen MatchPeriod = year_qtr if k2k == 1
        bysort cusip (year_qtr): egen MaxMatchPeriod = max(MatchPeriod)
        bysort cusip (year_qtr): egen MinMatchPeriod = min(MatchPeriod)
        format MatchPeriod MinMatchPeriod MaxMatchPeriod %tq
        gen count1 = year_qtr- MaxMatchPeriod
        gen count2 = year_qtr- MinMatchPeriod
        bysort cusip (year_qtr): egen MinCount = min(count2)
        gen count3 = count2 + abs(MinCount) + 1

        local ctrls = "c.LogSaleqL1_mz##c.LogSaleqL1_mz c.MBR##c.MBR c.DOI##c.DOI c.ppentq_qpc##c.ppentq_qpc c.FAtoSales_T4L##c.FAtoSales_T4L c.CIqL1##c.CIqL1"
        local macroctrls = ""
        bysort cusip (year_qtr): egen treatnowparallel = max(treatnow8)

        regress sdcogs8T_1 `ctrls' i.year_qtr `macroctrls' i.sic2 i.treatnowparallel##c.count3 if (count2 == -1 | count2 == -9 | count2 == -17 | count2 == -25) & cem_matched_ever == 1, vce(cluster cusip)
        outreg_Parallel_tex
        
    * Create data for table describing the observations ... copy the output into Excel and then save as a csv file to copy into the TEX file.
        tab year if treated8 == . & cem_matched == 1 & k2k == 1
        tab year if treated8 == 1 & cem_matched == 1 & k2k == 1
        tab sales_c if treated8 == . & cem_matched == 1 & k2k == 1
        tab sales_c if treated8 == 1 & cem_matched == 1 & k2k == 1
        tab sic2_c if treated8 == . & cem_matched == 1 & k2k == 1
        tab sic2_c if treated8 == 1 & cem_matched == 1 & k2k == 1

    * Identify control variables and moderators
        local ctrls = "c.LogSaleqL1_mz##c.LogSaleqL1_mz c.MBR##c.MBR c.DOI##c.DOI c.ppentq_qpc##c.ppentq_qpc c.FAtoSales_T4L##c.FAtoSales_T4L c.CIqL1##c.CIqL1"
        local macroctrls = ""

    * Run regressions
        regress sd_cogsDiff8 i.treatnow8 if vars_present8 == 1 & k2k == 1, vce(cluster cusip)
            outreg_Reg8_tex
    
        regress sd_cogsDiff8 i.treatnow8 `ctrls' if k2k == 1, vce(cluster cusip)
            outreg_Reg8_tex
            test LogSaleqL1_mz DOI MBR ppentq_qpc
    
        regress sd_cogsDiff8 i.treatnow8 `ctrls' i.year_qtr `macroctrls' i.sic2 if k2k == 1, vce(cluster cusip)
            outreg_Reg8_tex
            tab treatnow8 if e(sample)
            test LogSaleqL1_mz DOI MBR ppentq_qpc
            test `macroctrls'
            testparm i.year_qtr
            testparm i.sic2      

        gen treatnow = treatnow8
        regress sd_cogsDiff8 i.treatnow `ctrls' i.year_qtr `macroctrls' i.sic2 if cem_matched == 1 [iweight=cem_weights], vce(cluster cusip)
            outreg_RegNoMatch_tex

    * Run time trend analysis
        bysort cusip (year_qtr): gen count = _n
        sort cusip year_qtr
        gen LastChange = .
        replace LastChange = cond(k2k==1, count, cond(cusip == cusip[_n-1], LastChange[_n-1], .))
        
        gsort cusip -year_qtr
        gen NextChange = .
        replace NextChange = cond(k2k==1, count, cond(cusip == cusip[_n-1], NextChange[_n-1], .))
        sort cusip year_qtr
        order cusip year_qtr count LastChange NextChange
        
        gen Set = cond(LastChange == ., NextChange, LastChange)
        gen RelativePeriod = count - Set
        order cusip year_qtr count LastChange NextChange Set RelativePeriod
        
        gen tau0 = cond(RelativePeriod == 0, 1, 0)
        gen tauP1 = cond(RelativePeriod == 8, 1, 0)
        gen tauP2 = cond(RelativePeriod == 16, 1, 0)
        gen tauP3 = cond(RelativePeriod == 24, 1, 0)
        bysort cusip (year_qtr): egen k2kAllPeriods = max(k2k)
        regress sd_cogsDiff8 1i.treatever#1i.tau0 1i.treatever#1i.tauP1 1i.treatever#1i.tauP2 1i.treatever#1i.tauP3 `ctrls' i.year_qtr i.sic2 if k2kAllPeriods == 1 & (tau0 == 1 | tauP1 == 1 | tauP2 == 1 | tauP3 == 1) , vce(cluster cusip)
            lincom 1.treatever#1.tau0 +1.treatever#1.tauP1
            local estimate1 = `r(estimate)'
            local p1 = (2 * ttail(`r(df)', abs(`r(estimate)'/`r(se)')))
            lincom 1.treatever#1.tau0 + 1.treatever#1.tauP1 + 1.treatever#1.tauP2
            local estimate2 = `r(estimate)'
            local p2 = (2 * ttail(`r(df)', abs(`r(estimate)'/`r(se)')))
            lincom 1.treatever#1.tau0 + 1.treatever#1.tauP1 + 1.treatever#1.tauP2 + 1.treatever#1.tauP3
            local estimate3 = `r(estimate)'
            local p3 = (2 * ttail(`r(df)', abs(`r(estimate)'/`r(se)')))
            outreg_RegTrend_tex Q8NoOverlap "`estimate1'" "`estimate2'" "`estimate3'" "`p1'" "`p2'" "`p3'"

**** Save 
    compress
    save "3_Analysis.dta", replace
    
**** Save 
    keep if vars_present8 == 1 & k2k == 1 & FAtoSales_T4L != .
    keep sd_cogsDiff8 treatnow8 LogSaleqL1_mz MBR DOI ppentq_qpc FAtoSales_T4L CIqL1 saleq_tot14L ppentq_avg14L atq_avg14L year_qtr sic2 k2k cem_matched cusip gvkey_code  vars_present8 
    compress
    save "3_Analysis8.dta", replace

*/
*
capture erase 1_Reg12_tex.txt
capture erase 1_Reg12_tex.tex

**** Analysis - 12 quarter
**** Start with matched observations, merge in data with all variables to be used in regression analysis
    use "1_Variable_Prep.dta", clear
        keep if vars_present12 == 1

    merge 1:1 gvkey_code year_qtr using "2C_Matched_DatasetNoDedup.dta", keep(master match) gen(_CEM)
        tab treatnow _CEM

**** Model Free Check
    ttest sd1_cogs12 == sd2_cogs12 if cem_matched == 1 & treatnow12 == 1
    ttest sd_cogsDiff12 if cem_matched == 1 & k2k == 1, by(treatnow12)

    * There is no statistical difference in the pre-acquisition COGS variability between the treated12 and control firms
        ttest sdcogs12T_1 if cem_matched == 1 & k2k == 1, by(treatnow12)

    * Update treatever variable to identify firms that are treated
        drop treatever
        bysort cusip (year_qtr): egen treatever = max(treatnow12)
        tab treatnow12 treatever, missing
    
    * Identify all observations of a firm that was ever matched
        bysort cusip (year_qtr): egen cem_matched_ever = max(cem_matched)
        bysort cusip (year_qtr): egen k2k_ever = max(k2k)
        tab treatever cem_matched_ever, missing
        tab treatever k2k_ever, missing
            
    * Check validity of parallel trends assumption
        gen MatchPeriod = year_qtr if k2k == 1
        bysort cusip (year_qtr): egen MaxMatchPeriod = max(MatchPeriod)
        bysort cusip (year_qtr): egen MinMatchPeriod = min(MatchPeriod)
        format MatchPeriod MinMatchPeriod MaxMatchPeriod %tq
        gen count1 = year_qtr- MaxMatchPeriod
        gen count2 = year_qtr- MinMatchPeriod
        bysort cusip (year_qtr): egen MinCount = min(count2)
        gen count3 = count2 + abs(MinCount) + 1

        local ctrls = "c.LogSaleqL1_mz##c.LogSaleqL1_mz c.MBR##c.MBR c.DOI##c.DOI c.ppentq_qpc##c.ppentq_qpc c.FAtoSales_T4L##c.FAtoSales_T4L c.CIqL1##c.CIqL1"
        local macroctrls = ""
        bysort cusip (year_qtr): egen treatnowparallel = max(treatnow12)

        regress sdcogs12T_1 `ctrls' i.year_qtr `macroctrls' i.sic2 i.treatnowparallel##c.count3 if (count2 == -1 | count2 == -13 | count2 == -25 | count2 == -37) & cem_matched_ever == 1, vce(cluster cusip)
        outreg_Parallel_tex

    * Create data for table describing the observations ... copy the output into Excel and then save as a csv file to copy into the TEX file.
        tab year if treated12 == . & cem_matched == 1 & k2k == 1
        tab year if treated12 == 1 & cem_matched == 1 & k2k == 1
        tab sales_c if treated12 == . & cem_matched == 1 & k2k == 1
        tab sales_c if treated12 == 1 & cem_matched == 1 & k2k == 1
        tab sic2_c if treated12 == . & cem_matched == 1 & k2k == 1
        tab sic2_c if treated12 == 1 & cem_matched == 1 & k2k == 1

    * Identify control variables and moderators
        local ctrls = "c.LogSaleqL1_mz##c.LogSaleqL1_mz c.MBR##c.MBR c.DOI##c.DOI c.ppentq_qpc##c.ppentq_qpc c.FAtoSales_T4L##c.FAtoSales_T4L c.CIqL1##c.CIqL1"
        local macroctrls = ""
        
    * Run regressions
        regress sd_cogsDiff12 i.treatnow12 if vars_present12 == 1 & k2k == 1, vce(cluster cusip)
            outreg_Reg12_tex
    
        regress sd_cogsDiff12 i.treatnow12 `ctrls' if k2k == 1, vce(cluster cusip)
            outreg_Reg12_tex
            test LogSaleqL1_mz DOI MBR ppentq_qpc
    
        regress sd_cogsDiff12 i.treatnow12 `ctrls' i.year_qtr i.sic2 `macroctrls' if k2k == 1, vce(cluster cusip)
            outreg_Reg12_tex
            tab treatnow12 if e(sample)
            test LogSaleqL1_mz DOI MBR ppentq_qpc
            test `macroctrls'
            testparm i.year_qtr
            testparm i.sic2      

        gen treatnow = treatnow12
        regress sd_cogsDiff12 i.treatnow `ctrls' i.year_qtr `macroctrls' i.sic2 if cem_matched == 1 [iweight=cem_weights], vce(cluster cusip)
            outreg_RegNoMatch_tex

    * Run time trend analysis
        bysort cusip (year_qtr): gen count = _n
        sort cusip year_qtr
        gen LastChange = .
        replace LastChange = cond(k2k==1, count, cond(cusip == cusip[_n-1], LastChange[_n-1], .))
        
        gsort cusip -year_qtr
        gen NextChange = .
        replace NextChange = cond(k2k==1, count, cond(cusip == cusip[_n-1], NextChange[_n-1], .))
        sort cusip year_qtr
        order cusip year_qtr count LastChange NextChange
        
        gen Set = cond(LastChange == ., NextChange, LastChange)
        gen RelativePeriod = count - Set
        order cusip year_qtr count LastChange NextChange Set RelativePeriod
        
        gen tau0 = cond(RelativePeriod == 0, 1, 0)
        gen tauP1 = cond(RelativePeriod == 12, 1, 0)
        gen tauP2 = cond(RelativePeriod == 24, 1, 0)
        gen tauP3 = cond(RelativePeriod == 36, 1, 0)
        bysort cusip (year_qtr): egen k2kAllPeriods = max(k2k)
        regress sd_cogsDiff12 1i.treatever#1i.tau0 1i.treatever#1i.tauP1 1i.treatever#1i.tauP2 1i.treatever#1i.tauP3 `ctrls' i.year_qtr i.sic2 if k2kAllPeriods == 1 & (tau0 == 1 | tauP1 == 1 | tauP2 == 1 | tauP3 == 1) , vce(cluster cusip)
            lincom 1.treatever#1.tau0 +1.treatever#1.tauP1
            local estimate1 = `r(estimate)'
            local p1 = (2 * ttail(`r(df)', abs(`r(estimate)'/`r(se)')))
            lincom 1.treatever#1.tau0 + 1.treatever#1.tauP1 + 1.treatever#1.tauP2
            local estimate2 = `r(estimate)'
            local p2 = (2 * ttail(`r(df)', abs(`r(estimate)'/`r(se)')))
            lincom 1.treatever#1.tau0 + 1.treatever#1.tauP1 + 1.treatever#1.tauP2 + 1.treatever#1.tauP3
            local estimate3 = `r(estimate)'
            local p3 = (2 * ttail(`r(df)', abs(`r(estimate)'/`r(se)')))
            outreg_RegTrend_tex Q12NoOverlap "`estimate1'" "`estimate2'" "`estimate3'" "`p1'" "`p2'" "`p3'"

**** Save 
    keep if vars_present12 == 1 & k2k == 1 & FAtoSales_T4L != .
    keep sd_cogsDiff12 treatnow12 LogSaleqL1_mz MBR DOI ppentq_qpc FAtoSales_T4L CIqL1 saleq_tot14L ppentq_avg14L atq_avg14L year_qtr sic2 k2k cem_matched cusip gvkey_code  vars_present12
    compress
    save "3_Analysis12.dta", replace

capture log close
