// domestic value_added based on Hiau looikee and Heiwai Tang (2016) //

// "E:\Research\GVCs\data\impute_kww2012"

use "E:\Research\GVCs\data\domestic_value_firm\IO_GB.dta"
merge m:1 IO_id using "E:\Research\GVCs\data\KWW12\BEC-classicification\only_2007\IO-IO_code.dta"
drop if _merge==1 | _merge==2
rename GB_code indu_code
drop _merge
duplicates drop indu_code,force
save "E:\Research\GVCs\data\domestic_value_firm\IO_GB.dta", replace

// basic environment setting //
clear
set more off
global PATH1 "E:\Research\GVCs\data\Merge_AIS_and_CTL"
cd $PATH1

// domestic value-addded ration of processing trade //
// generate DVAR of processing trade //

use merged_data_2002-2007.dta
//drop if exp_impid_ex!=exp_impid_im

// merge the data of KWW2012 //
merge m:1 indu_code using "E:\Research\GVCs\data\domestic_value_firm\IO_GB.dta" // identify indu classification
drop _merge
merge m:1 IO   year using "E:\Research\GVCs\data\impute_kww2012\n_2002-2007.dta"
drop _merge

// merge the imported capital 
sort companyname
merge m:1 companyname year using  "E:\Research\GVCs\data\Merge_AIS_and_CTL\captal_per_year_p.dta"
drop _merge

// drop the excessive importer
drop if import>intermediate*1000 & import!=. & intermediate!=.

gen dvar=(1-(import/export))*(1-fva_indrect_p) if exp_impid_ex=="1"
drop if dvar==.
drop if dvar<0

save "E:\Research\GVCs\data\domestic_value_firm\workhouse_p.dta",replace


use merged_data_2002-2007.dta
//drop if exp_impid_ex!=exp_impid_im
// merge the data of KWW2012 //
merge m:1 indu_code using "E:\Research\GVCs\data\domestic_value_firm\IO_GB.dta" // identify indu classification
drop _merge
merge m:1 IO   year using "E:\Research\GVCs\data\impute_kww2012\n_2002-2007.dta"
drop _merge

// merge the imported capital 
sort companyname
merge m:1 companyname year using  "E:\Research\GVCs\data\Merge_AIS_and_CTL\captal_per_year_o.dta"
drop _merge
replace total_cap_o=0 if total_cap_o==.
replace import=0 if import==.

gen dvar=(1-(import+total_cap_o)/main_income*1000)*(1-fva_indrect_d) if exp_impid_ex=="0" & export!=. & export!=0

drop if dvar==.
drop if dvar<0
sum dvar
tabstat dvar , ///
        stats (count mean sd median max min  p25  )

save "E:\Research\GVCs\data\domestic_value_firm\workhouse_o.dta",replace

use "E:\Research\GVCs\data\domestic_value_firm\workhouse_p.dta"

save "E:\Research\GVCs\data\domestic_value_firm\workhouse_p.dta",replace









