**** 6C_Analysis.do - Conducts DESEASONALIZED regressions for 8-qtr ADD, DROP and SWAP

capture log close
log using "6C_Analysis_log.txt", text replace
set more off
clear all

capture erase 6C_RegAdd_tex.txt
capture erase 6C_RegAdd_tex.tex
capture erase 6C_RegDrop_tex.txt
capture erase 6C_RegDrop_tex.tex
capture erase 6C_RegSwap_tex.txt
capture erase 6C_RegSwap_tex.tex

	program outreg_RegAdd_tex
        count if e(sample) & treatnow == 1
        local num_treat = `r(N)'
        unique cusip if e(sample)
        local num_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 6C_RegAdd_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_RegDrop_tex
        count if e(sample) & treatnow == 1
        local num_treat = `r(N)'
        unique cusip if e(sample)
        local num_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 6C_RegDrop_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_RegSwap_tex
        count if e(sample) & treatnow == 1
        local num_treat = `r(N)'
        unique cusip if e(sample)
        local num_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 6C_RegSwap_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

*********************************************************************************************************
**** ADD Analysis - 4 quarter
*********************************************************************************************************
**** Start with matched observations, merge in data with all variables to be used in regression analysis
    use "6A_Variable_Prep.dta", clear

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

**** Model Free Check
    * 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

    * 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 treatnow4 == 0 & cem_matched == 1 & k2k == 1
        tab year if treatnow4 == 1 & cem_matched == 1 & k2k == 1
        tab sales_c if treatnow4 == 0 & cem_matched == 1 & k2k == 1
        tab sales_c if treatnow4 == 1 & cem_matched == 1 & k2k == 1
        tab sic2_c if treatnow4 == 0 & cem_matched == 1 & k2k == 1
        tab sic2_c if treatnow4 == 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"

    * Run regressions
        gen treatnow = treatnow4
        regress sd_cogsDiff4 i.treatnow `ctrls' i.year_qtr i.sic2 if k2k == 1, vce(cluster cusip)
            outreg_RegAdd_tex
            tab treatnow4 if e(sample)
            test LogSaleqL1_mz DOI MBR ppentq_qpc

**** Save
    keep if vars_present4 == 1 & k2k == 1 & FAtoSales_T4L != .
    keep sd_cogsDiff4 treatnow4 LogSaleqL1_mz MBR DOI ppentq_qpc FAtoSales_T4L CIqL1 saleq_tot14L year_qtr sic2 k2k cem_matched cusip  vars_present4
    compress
    save "6C_AnalysisAdd4.dta", replace


*********************************************************************************************************
**** ADD Analysis - 8 quarter
*********************************************************************************************************
**** Start with matched observations, merge in data with all variables to be used in regression analysis
    use "6A_Variable_Prep.dta", clear

    merge 1:1 gvkey_code year_qtr using "6B_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)

    * 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 
        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

    * 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 treatnow8 == 0 & cem_matched == 1 & k2k == 1
        tab year if treatnow8 == 1 & cem_matched == 1 & k2k == 1
        tab sales_c if treatnow8 == 0 & cem_matched == 1 & k2k == 1
        tab sales_c if treatnow8 == 1 & cem_matched == 1 & k2k == 1
        tab sic2_c if treatnow8 == 0 & cem_matched == 1 & k2k == 1
        tab sic2_c if treatnow8 == 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"

    * Run regressions
        gen treatnow = treatnow8
        regress sd_cogsDiff8 i.treatnow `ctrls' i.year_qtr i.sic2 if k2k == 1, vce(cluster cusip)
            outreg_RegAdd_tex
            tab treatnow8 if e(sample)
            test LogSaleqL1_mz DOI MBR ppentq_qpc

**** 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 year_qtr sic2 k2k cem_matched cusip  vars_present8
    compress
    save "6C_AnalysisAdd8.dta", replace

*********************************************************************************************************
**** ADD Analysis - 12 quarter
*********************************************************************************************************
**** Start with matched observations, merge in data with all variables to be used in regression analysis
    use "6A_Variable_Prep.dta", clear

    merge 1:1 gvkey_code year_qtr using "6B_Matched_DatasetNoDedup 12Qtr.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)

    * 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 
        sum sdcogs12T_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(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

    * 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 treatnow12 == 0 & cem_matched == 1 & k2k == 1
        tab year if treatnow12 == 1 & cem_matched == 1 & k2k == 1
        tab sales_c if treatnow12 == 0 & cem_matched == 1 & k2k == 1
        tab sales_c if treatnow12 == 1 & cem_matched == 1 & k2k == 1
        tab sic2_c if treatnow12 == 0 & cem_matched == 1 & k2k == 1
        tab sic2_c if treatnow12 == 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"

    * Run regressions
        gen treatnow = treatnow12
        regress sd_cogsDiff12 i.treatnow `ctrls' i.year_qtr i.sic2 if k2k == 1, vce(cluster cusip)
            outreg_RegAdd_tex
            tab treatnow12 if e(sample)
            test LogSaleqL1_mz DOI MBR ppentq_qpc

**** 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 year_qtr sic2 k2k cem_matched cusip  vars_present12
    compress
    save "6C_AnalysisAdd12.dta", replace


*********************************************************************************************************
**** DROP Analysis - 4 quarter
*********************************************************************************************************
**** Start with matched observations, merge in data with all variables to be used in regression analysis
    use "6A_Variable_Prep.dta", clear

    merge 1:1 gvkey_code year_qtr using "6B_Matched_drop_DatasetNoDedup 4Qtr.dta", keep(master match) gen(_CEM)
        tab treatnow4 _CEM

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

**** Model Free Check
    ttest sd1_cogs4drop == sd2_cogs4drop if cem_matched == 1 & treatnow4 == 1
    ttest sd_cogsDiff4drop 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

    * 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 treatnow4 == 0 & cem_matched == 1 & k2k == 1
        tab year if treatnow4 == 1 & cem_matched == 1 & k2k == 1
        tab sales_c if treatnow4 == 0 & cem_matched == 1 & k2k == 1
        tab sales_c if treatnow4 == 1 & cem_matched == 1 & k2k == 1
        tab sic2_c if treatnow4 == 0 & cem_matched == 1 & k2k == 1
        tab sic2_c if treatnow4 == 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"
        * nmissing `ctrls'

        gen treatnow = treatnow4
        regress sd_cogsDiff4drop i.treatnow `ctrls' i.year_qtr i.sic2 if k2k == 1, vce(cluster cusip)
            outreg_RegDrop_tex
            tab treatnow4 if e(sample)
            test LogSaleqL1_mz DOI MBR ppentq_qpc

**** Save
    keep if vars_present4_drop == 1 & k2k == 1 & FAtoSales_T4L != .
    keep sd_cogsDiff4drop treatnow4 LogSaleqL1_mz MBR DOI ppentq_qpc FAtoSales_T4L CIqL1 saleq_tot14L year_qtr sic2 k2k cem_matched cusip  vars_present4_drop
    compress

    save "6C_Analysis4_drop.dta", replace

*********************************************************************************************************
**** DROP Analysis - 8 quarter
*********************************************************************************************************
**** Start with matched observations, merge in data with all variables to be used in regression analysis
    use "6A_Variable_Prep.dta", clear

    merge 1:1 gvkey_code year_qtr using "6B_Matched_drop_DatasetNoDedup.dta", keep(master match) gen(_CEM)
        tab treatnow8 _CEM

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

**** Model Free Check
    ttest sd1_cogs8drop == sd2_cogs8drop if cem_matched == 1 & treatnow8 == 1
    ttest sd_cogsDiff8drop if cem_matched == 1 & k2k == 1, by(treatnow8)

    * 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

    * 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 treatnow8 == 0 & cem_matched == 1 & k2k == 1
        tab year if treatnow8 == 1 & cem_matched == 1 & k2k == 1
        tab sales_c if treatnow8 == 0 & cem_matched == 1 & k2k == 1
        tab sales_c if treatnow8 == 1 & cem_matched == 1 & k2k == 1
        tab sic2_c if treatnow8 == 0 & cem_matched == 1 & k2k == 1
        tab sic2_c if treatnow8 == 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"
        
        
    * Run regressions
        gen treatnow = treatnow8
        regress sd_cogsDiff8drop i.treatnow `ctrls' i.year_qtr i.sic2 if k2k == 1, vce(cluster cusip)
            outreg_RegDrop_tex
            tab treatnow8 if e(sample)
            test LogSaleqL1_mz DOI MBR ppentq_qpc

**** Save
    keep if vars_present8_drop == 1 & k2k == 1 & FAtoSales_T4L != .
    keep sd_cogsDiff8drop treatnow8 LogSaleqL1_mz MBR DOI ppentq_qpc FAtoSales_T4L CIqL1 saleq_tot14L year_qtr sic2 k2k cem_matched cusip  vars_present8_drop
    compress

    save "6C_Analysis8_drop.dta", replace

*********************************************************************************************************
**** DROP Analysis - 12 quarter
*********************************************************************************************************
**** Start with matched observations, merge in data with all variables to be used in regression analysis
    use "6A_Variable_Prep.dta", clear

    merge 1:1 gvkey_code year_qtr using "6B_Matched_drop_DatasetNoDedup 12Qtr.dta", keep(master match) gen(_CEM)
        tab treatnow12 _CEM

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

**** Model Free Check
    ttest sd1_cogs12drop == sd2_cogs12drop if cem_matched == 1 & treatnow12 == 1
    ttest sd_cogsDiff12drop 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

    * 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 treatnow12 == 0 & cem_matched == 1 & k2k == 1
        tab year if treatnow12 == 1 & cem_matched == 1 & k2k == 1
        tab sales_c if treatnow12 == 0 & cem_matched == 1 & k2k == 1
        tab sales_c if treatnow12 == 1 & cem_matched == 1 & k2k == 1
        tab sic2_c if treatnow12 == 0 & cem_matched == 1 & k2k == 1
        tab sic2_c if treatnow12 == 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"

    * Run regressions
        gen treatnow = treatnow12
        regress sd_cogsDiff12drop i.treatnow `ctrls' i.year_qtr i.sic2 if k2k == 1, vce(cluster cusip)
            outreg_RegDrop_tex
            tab treatnow12 if e(sample)
            test LogSaleqL1_mz DOI MBR ppentq_qpc

**** Save
    keep if vars_present12_drop == 1 & k2k == 1 & FAtoSales_T4L != .
    keep sd_cogsDiff12drop treatnow12 LogSaleqL1_mz MBR DOI ppentq_qpc FAtoSales_T4L CIqL1 saleq_tot14L year_qtr sic2 k2k cem_matched cusip  vars_present12_drop
    compress

    save "6C_Analysis12_drop.dta", replace

*********************************************************************************************************
**** SWAP Analysis - 4 quarter
*********************************************************************************************************
**** Start with matched observations, merge in data with all variables to be used in regression analysis
    use "6A_Variable_Prep.dta", clear

    merge 1:1 gvkey_code year_qtr using "6B_Matched_as_DatasetNoDedup 4Qtr.dta", keep(master match) gen(_CEM)
        tab treatnow _CEM

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

**** Model Free Check
    ttest sd1_cogs4as == sd2_cogs4as if cem_matched == 1 & treatnow4 == 1
    ttest sd_cogsDiff4as 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

    * 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 treatnow4 == 0 & cem_matched == 1 & k2k == 1
        tab year if treatnow4 == 1 & cem_matched == 1 & k2k == 1
        tab sales_c if treatnow4 == 0 & cem_matched == 1 & k2k == 1
        tab sales_c if treatnow4 == 1 & cem_matched == 1 & k2k == 1
        tab sic2_c if treatnow4 == 0 & cem_matched == 1 & k2k == 1
        tab sic2_c if treatnow4 == 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"

    * Run regressions
        gen treatnow = treatnow4
        regress sd_cogsDiff4as i.treatnow `ctrls' i.year_qtr i.sic2 if k2k == 1, vce(cluster cusip)
            outreg_RegSwap_tex
            tab treatnow4 if e(sample)
            test LogSaleqL1_mz DOI MBR ppentq_qpc

**** Save
    keep if vars_present4_as == 1 & k2k == 1 & FAtoSales_T4L != .
    keep sd_cogsDiff4as treatnow4 LogSaleqL1_mz MBR DOI ppentq_qpc FAtoSales_T4L CIqL1 saleq_tot14L year_qtr sic2 k2k cem_matched cusip  vars_present4_as
    compress
    save "6C_Analysis4_as.dta", replace

*********************************************************************************************************
**** SWAP Analysis - 8 quarter
*********************************************************************************************************
**** Start with matched observations, merge in data with all variables to be used in regression analysis
    use "6A_Variable_Prep.dta", clear

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

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

**** Model Free Check
    ttest sd1_cogs8as == sd2_cogs8as if cem_matched == 1 & treatnow8 == 1
    ttest sd_cogsDiff8as if cem_matched == 1 & k2k == 1, by(treatnow8)

    * 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

    * 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 treatnow8 == 0 & cem_matched == 1 & k2k == 1
        tab year if treatnow8 == 1 & cem_matched == 1 & k2k == 1
        tab sales_c if treatnow8 == 0 & cem_matched == 1 & k2k == 1
        tab sales_c if treatnow8 == 1 & cem_matched == 1 & k2k == 1
        tab sic2_c if treatnow8 == 0 & cem_matched == 1 & k2k == 1
        tab sic2_c if treatnow8 == 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"

    * Run regressions
        gen treatnow = treatnow8
        regress sd_cogsDiff8as i.treatnow `ctrls' i.year_qtr i.sic2 if k2k == 1, vce(cluster cusip)
            outreg_RegSwap_tex
            tab treatnow8 if e(sample)
            test LogSaleqL1_mz DOI MBR ppentq_qpc

**** Save
    keep if vars_present8_as == 1 & k2k == 1 & FAtoSales_T4L != .
    keep sd_cogsDiff8as treatnow8 LogSaleqL1_mz MBR DOI ppentq_qpc FAtoSales_T4L CIqL1 saleq_tot14L year_qtr sic2 k2k cem_matched cusip  vars_present8_as
    compress
    save "6C_Analysis8_as.dta", replace

*********************************************************************************************************
**** SWAP Analysis - 12 quarter
*********************************************************************************************************
**** Start with matched observations, merge in data with all variables to be used in regression analysis
    use "6A_Variable_Prep.dta", clear

    merge 1:1 gvkey_code year_qtr using "6B_Matched_as_DatasetNoDedup 12Qtr.dta", keep(master match) gen(_CEM)
        tab treatnow _CEM

    * 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)

**** Model Free Check
    ttest sd1_cogs12as == sd2_cogs12as if cem_matched == 1 & treatnow12 == 1
    ttest sd_cogsDiff12as 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

    * 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 treatnow12 == 0 & cem_matched == 1 & k2k == 1
        tab year if treatnow12 == 1 & cem_matched == 1 & k2k == 1
        tab sales_c if treatnow12 == 0 & cem_matched == 1 & k2k == 1
        tab sales_c if treatnow12 == 1 & cem_matched == 1 & k2k == 1
        tab sic2_c if treatnow12 == 0 & cem_matched == 1 & k2k == 1
        tab sic2_c if treatnow12 == 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"

    * Run regressions
        gen treatnow = treatnow12
        regress sd_cogsDiff12as i.treatnow `ctrls' i.year_qtr i.sic2 if k2k == 1, vce(cluster cusip)
            outreg_RegSwap_tex
            tab treatnow12 if e(sample)
            test LogSaleqL1_mz DOI MBR ppentq_qpc
            test `macroctrls'
            testparm i.year_qtr
            testparm i.sic2

 **** Save
    keep if vars_present12_as == 1 & k2k == 1 & FAtoSales_T4L != .
    keep sd_cogsDiff12as treatnow12 LogSaleqL1_mz MBR DOI ppentq_qpc FAtoSales_T4L CIqL1 saleq_tot14L year_qtr sic2 k2k cem_matched cusip  vars_present12_as
    compress
    save "6C_Analysis12_as.dta", replace

capture log close
