**** 3_Analysis_CapUtil_Moderators.do - Conducts Capacity Utilization moderator analysis for ADD, DROP and SWAP

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

capture erase 1_Add_Moderators_tex.txt
capture erase 1_Add_Moderators_tex.tex
capture erase 1_Drop_Moderators_tex.txt
capture erase 1_Drop_Moderators_tex.tex
capture erase 1_Swap_Moderators_tex.txt
capture erase 1_Swap_Moderators_tex.tex

	program outreg_Add_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 CogsPPEL1 1.treatnow#c.CogsPPEL1 LogSaleqL1_mz c.LogSaleqL1_mz#c.LogSaleqL1_mz DOI c.DOI#c.DOI MBR c.MBR#c.MBR ppentq_qpc c.ppentq_qpc#c.ppentq_qpc FAtoSales_T4L c.FAtoSales_T4L#c.FAtoSales_T4L CIqL1 c.CIqL1#c.CIqL1"
        outreg2 using 1_Add_Moderators_tex, tex(fragment) addstat(Treated Observations, `num_treat') alpha(0.01, 0.05, 0.1) symbol(***, **, *) bracket dec(4) label ctitle(`1') keep(`keep_var1') sortvar(`keep_var1') fmt(f)
    end

	program outreg_Drop_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 CogsPPEL1 1.treatnow#c.CogsPPEL1 LogSaleqL1_mz c.LogSaleqL1_mz#c.LogSaleqL1_mz DOI c.DOI#c.DOI MBR c.MBR#c.MBR ppentq_qpc c.ppentq_qpc#c.ppentq_qpc FAtoSales_T4L c.FAtoSales_T4L#c.FAtoSales_T4L CIqL1 c.CIqL1#c.CIqL1"
        outreg2 using 1_Drop_Moderators_tex, tex(fragment) addstat(Treated Observations, `num_treat') alpha(0.01, 0.05, 0.1) symbol(***, **, *) bracket dec(4) label ctitle(`1') keep(`keep_var1') sortvar(`keep_var1') fmt(f)
    end

	program outreg_Swap_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 CogsPPEL1 1.treatnow#c.CogsPPEL1 LogSaleqL1_mz c.LogSaleqL1_mz#c.LogSaleqL1_mz DOI c.DOI#c.DOI MBR c.MBR#c.MBR ppentq_qpc c.ppentq_qpc#c.ppentq_qpc FAtoSales_T4L c.FAtoSales_T4L#c.FAtoSales_T4L CIqL1 c.CIqL1#c.CIqL1"
        outreg2 using 1_Swap_Moderators_tex, tex(fragment) addstat(Treated Observations, `num_treat') alpha(0.01, 0.05, 0.1) symbol(***, **, *) bracket dec(4) label ctitle(`1') keep(`keep_var1') sortvar(`keep_var1')
    end

**** Analysis
**** Start with matched observations, merge in data with all variables to be used in regression analysis
    use "1_Variable_Prep.dta", clear

    merge 1:1 gvkey_code year_qtr using "2A_Matched_DatasetNoDedup.dta", keep(master match) keepusing(treatnow4 k2k cem_matched) gen(_CEM4add)
        tab treatnow4 _CEM4add
        rename treatnow4 treatnow4add
        rename k2k k2k4add
        rename cem_matched cem_matched4add

    merge 1:1 gvkey_code year_qtr using "2B_Matched_DatasetNoDedup.dta", keep(master match) keepusing(treatnow8 k2k cem_matched) gen(_CEM8add)
        tab treatnow8 _CEM8add
        rename treatnow8 treatnow8add
        rename k2k k2k8add
        rename cem_matched cem_matched8add

    merge 1:1 gvkey_code year_qtr using "2C_Matched_DatasetNoDedup.dta", keep(master match) keepusing(treatnow12 k2k cem_matched) gen(_CEM12add)
        tab treatnow12 _CEM12add
        rename treatnow12 treatnow12add
        rename k2k k2k12add
        rename cem_matched cem_matched12add

    merge 1:1 gvkey_code year_qtr using "2A_Matched_drop_DatasetNoDedup.dta", keep(master match) keepusing(treatnow4 k2k cem_matched) gen(_CEM4drop)
        tab treatnow4 _CEM4drop
        rename treatnow4 treatnow4drop
        rename k2k k2k4drop
        rename cem_matched cem_matched4drop

    merge 1:1 gvkey_code year_qtr using "2B_Matched_drop_DatasetNoDedup.dta", keep(master match) keepusing(treatnow8 k2k cem_matched) gen(_CEM8drop)
        tab treatnow8 _CEM8drop
        rename treatnow8 treatnow8drop
        rename k2k k2k8drop
        rename cem_matched cem_matched8drop

    merge 1:1 gvkey_code year_qtr using "2C_Matched_drop_DatasetNoDedup.dta", keep(master match) keepusing(treatnow12 k2k cem_matched) gen(_CEM12drop)
        tab treatnow12 _CEM12drop
        rename treatnow12 treatnow12drop
        rename k2k k2k12drop
        rename cem_matched cem_matched12drop

    merge 1:1 gvkey_code year_qtr using "2A_Matched_as_DatasetNoDedup.dta", keep(master match) keepusing(treatnow4 k2k cem_matched) gen(_CEM4swap)
        tab treatnow4 _CEM4swap
        rename treatnow4 treatnow4swap
        rename k2k k2k4swap
        rename cem_matched cem_matched4swap

    merge 1:1 gvkey_code year_qtr using "2B_Matched_as_DatasetNoDedup.dta", keep(master match) keepusing(treatnow8 k2k cem_matched) gen(_CEM8swap)
        tab treatnow8 _CEM8swap
        rename treatnow8 treatnow8swap
        rename k2k k2k8swap
        rename cem_matched cem_matched8swap

    merge 1:1 gvkey_code year_qtr using "2C_Matched_as_DatasetNoDedup.dta", keep(master match) keepusing(treatnow12 k2k cem_matched) gen(_CEM12swap)
        tab treatnow12 _CEM12swap
        rename treatnow12 treatnow12swap
        rename k2k k2k12swap
        rename cem_matched cem_matched12swap

        
    * Asset Utlization
        egen ppentqavg12L = rowmean(ppentqL1 ppentqL2)
        gen CogsPPEL1= saleqL1 / ppentqavg12L
        label variable CogsPPEL1 "Asset Utilization"

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

        gen treatnow = treatnow4add

    * Run regressions
        *4 Quarter
        replace treatnow = treatnow4add
        regress sd_cogsDiff4 i.year_qtr i.sic2 `ctrls' i.treatnow##c.CogsPPEL1 if k2k4add == 1, vce(cluster cusip)
            outreg_Add_tex Add4Qtr
                
        replace treatnow = treatnow4drop
        regress sd_cogsDiff4drop i.year_qtr i.sic2 `ctrls' i.treatnow##c.CogsPPEL1 if k2k4drop == 1, vce(cluster cusip)
            outreg_Drop_tex Drop4Qtr

        replace treatnow = treatnow4swap
        regress sd_cogsDiff4as i.year_qtr i.sic2 `ctrls' i.treatnow##c.CogsPPEL1 if k2k4swap == 1, vce(cluster cusip)
            outreg_Swap_tex Swap4Qtr
        
        * 8 Quarter
        replace treatnow = treatnow8add
        regress sd_cogsDiff8 i.year_qtr i.sic2 `ctrls' i.treatnow##c.CogsPPEL1 if k2k8add == 1, vce(cluster cusip)
            outreg_Add_tex Add8Qtr
                
        replace treatnow = treatnow8drop
        regress sd_cogsDiff8drop i.year_qtr i.sic2 `ctrls' i.treatnow##c.CogsPPEL1 if k2k8drop == 1, vce(cluster cusip)
            outreg_Drop_tex Drop8Qtr

        replace treatnow = treatnow8swap
        regress sd_cogsDiff8as i.year_qtr i.sic2 `ctrls' i.treatnow##c.CogsPPEL1 if k2k8swap == 1, vce(cluster cusip)
             outreg_Swap_tex Swap8Qtr
       
        * 12 Quarter
        replace treatnow = treatnow12add
        regress sd_cogsDiff12 i.year_qtr i.sic2 `ctrls' i.treatnow##c.CogsPPEL1 if k2k12add == 1, vce(cluster cusip)
            outreg_Add_tex Add12Qtr

        replace treatnow = treatnow12drop
        regress sd_cogsDiff12drop i.year_qtr i.sic2 `ctrls' i.treatnow##c.CogsPPEL1 if k2k12drop == 1, vce(cluster cusip)
            outreg_Drop_tex Drop12Qtr

        replace treatnow = treatnow12swap
        regress sd_cogsDiff12as i.year_qtr i.sic2 `ctrls' i.treatnow##c.CogsPPEL1 if k2k12swap == 1, vce(cluster cusip)
            outreg_Swap_tex Swap12Qtr


**** Save 
    compress
    save "3_AnalysisCapUtilModerator.dta", replace

capture log close
