clear
set more off
global PATH1 "E:\Research\GVCs\data\financial_constraint\by_year_upstreamness2"
cd $PATH1
 
use workhouse_bench.dta	  


merge m:1 IO using alpha_90.dta
drop _merge

tostring IO,gen(IO_s) force
tostring year,gen(y) force
gen merge_code=IO_s+y
drop IO_s y
merge m:1 merge_code using up2_fin.dta,force
drop _merge

sum alpha
return list
gen x=r(max)

sort alpha
_pctile alpha, nq(4)
return list   

gen alpha_0=1 if alpha<r(r1)
replace alpha_0=0 if alpha_0==.	

forvalues i=1(1)2{
  local j=`i'+1
  gen alpha_`i'=1 if alpha>=r(r`i') & alpha<r(r`j')
  replace alpha_`i'=0 if alpha_`i'==.
}	
  
gen alpha_3=1 if alpha>=r(r3) & alpha<x
replace alpha_3=0 if alpha_3==.


forvalues i=0(1)3{
gen fin1_up_alpha_`i'=alpha_`i'*upstream_fin1
}

forvalues i=0(1)3{
gen fin2_up_alpha_`i'=alpha_`i'*upstream_fin2
}

forvalues i=0(1)3{
gen fin3_up_alpha_`i'=alpha_`i'*upstream_fin3
}

forvalues i=0(1)3{
gen fin4_up_alpha_`i'=alpha_`i'*upstream_fin4
}

duplicates drop id year,force
xtset id year
xtreg dvar     fin1_up_alpha_*     if dvar<0.985,fe robust
est store m1
xtreg dvar     fin2_up_alpha_*     if dvar<0.985,fe robust
est store m2
xtreg dvar     fin4_up_alpha_*     if dvar<0.985,fe robust
est store m3
esttab  m1 m2 m3  using T_1.rtf, ///
   p star(* 0.1 ** 0.05 *** 0.01) r2  nogap replace   

    
   


 
 
 
 
