capture log close
log using modelfree_drop.txt, text replace
set more off
clear all
*capture erase drop.xls

use "Revere_quarterly.dta", clear
	keep s_cusip c_cusip datacqtr
	replace s_cusip=substr(s_cusip,1,6)
	replace c_cusip=substr(c_cusip,1,6)
	drop if mi(s_cusip)|mi(c_cusip)
	duplicates report s_cusip c_cusip datacqtr
	duplicates drop s_cusip c_cusip datacqtr,force

	*change datacqtr to datetime var in stata
	gen year=floor(datacqtr/10)
    gen qtr=datacqtr-year*10
    replace datacqtr = yq(year, qtr)
    format datacqtr %tq

* Increase in #c definition
    order c_cusip s_cusip datacqtr
    bysort c_cusip s_cusip (datacqtr): gen count = _n
    
    bysort c_cusip s_cusip: egen min = min(count)
    bysort c_cusip s_cusip: egen max = max(count)
    gen firstdate = cond(count == min, 1, 0)
    gen lastdate = cond(count == max, 1, 0)
    
    bysort s_cusip datacqtr: egen Added = sum(firstdate)
    bysort s_cusip datacqtr: egen Lost = sum(lastdate)
    
    save "custchanges_drop.dta", replace
    drop count c_cusip firstdate lastdate min max
    duplicates drop
    duplicates tag s_cusip datacqtr, gen(dup)
    sum dup
    assert `r(max)' ==  0
    drop dup
    
    bysort s_cusip (datacqtr): gen count = _n
    bysort s_cusip (datacqtr): egen max = max(count)
    bysort s_cusip (datacqtr): egen min = min(count)
	
	replace Added = 0 if count == min
    replace Lost = 0 if count == max
    
	gen jump1=Added>0 & Added!=.   //jump1 is defined as having customer(s) add in current quarter
	gen jump2=Lost>0 & Lost!=.     //jump2 is defined as having customer(s) drop in the next quarter

	drop Added Lost count max min
	
	* Set the "Drop" as main event
	rename jump2 main_jump
	rename jump1 other_jump
	
	save "alljumps.dta", replace
	
	keep if main_jump
	drop other_jump

forvalues k = 4(4)12 {
    
	preserve
	
*** Delete overlap, [-k,k] window with Drop duplications
	bys s_cusip (datacqtr): gen To_last = datacqtr - datacqtr[_n-1]
	bys s_cusip (datacqtr): gen To_next = datacqtr[_n+1] - datacqtr
	
    gen if_overlap=To_last<=`k' | To_next<=`k'
    drop if if_overlap
    drop if_overlap To_last To_next
	
* the beginning quarter is 200302, so no jumps should be defined before 200402
    drop if datacqtr < tq(2004q2)

    gen jumpqtr=datacqtr
	format jumpqtr %tq
    egen n_event=group(s_cusip datacqtr)
	
* expand to [-k-1,k] window

    local d = 2 * `k' + 2
    expand `d'
    sort s_cusip datacqtr
    bys s_cusip datacqtr: gen n = _n
	replace datacqtr = datacqtr + n -`k'- 2

	drop n qtr
	replace main_jump = 0 if datacqtr!= jumpqtr

* Calculate log-differenced supplier COGS (pre/post) and t-test
	merge m:1 s_cusip datacqtr using "compustat_s_naics3-4.dta"
	keep if _merge==3
	drop _merge
	egen ns=group(s_cusip n_event)
	
	xtset ns datacqtr
	gen ld_cogsq = ln(sup_cogsq)-ln(l.sup_cogsq)
	gen pre_t0= datacqtr< jumpqtr
	gen post_t0= datacqtr> jumpqtr
	egen sd_cogsq = sd(ld_cogsq) if pre_t0, by(ns)
	egen sd1_cogs=max(sd_cogsq), by(ns)
	egen sd_cogsq2 = sd(ld_cogsq) if post_t0, by(ns)
	egen sd2_cogs=max(sd_cogsq2), by(ns)
	duplicates drop ns, force	
	tostring qtr,replace
    save "mf_drop`k'", replace

    display "modelfree_DROP"
	display "Non-overlap Window = [-`k',`k']"
	
	ttest sd1_cogs == sd2_cogs
	signtest sd1_cogs = sd2_cogs
	signrank sd1_cogs = sd2_cogs

	restore

}
//estout * using drop.xls, starlevels(* 0.10 ** 0.05 *** 0.01) cells(b t(star fmt(4))) stats(N mean_sd1 mean_sd2)
log close
