Stata - 创建一组所有可能的不匹配对

Stata - Creating a set of all possible unmatched pairs

我有观察到的匹配对(供应商-买家)的观察数据,我想用它来建立一组反事实观察。 假设我们观察两个供应商(X 和 Y)和 3 个买家(A、B 和 C)。 我们观察到供应商 X 与买方 A 签订了 200 万美元的合同,与买方 B 签订了 500 万美元的合同。 我们还观察到供应商 Y 只与买方 C 签订了一份价值 400 万美元的合同。 我想根据一组不匹配的对构建一组反事实。我想为每个反事实观察设置一个假设的合同价值,该合同价值等于每一方在观察到的数据中签订的合同的最大值。例如,这是我们根据上述观察得到的反事实:

反事实观察 #1:X 和 C 的价值为 500 万美元(因为 X 观察到的最大合同有 500 万美元,而 C 有 400 万美元,500 万美元更高) 反事实观察 #2:Y 与 A 的价值为 400 万美元(因为 Y 有 400 万美元用于他观察到的最大合同,而 A 有 200 万美元,400 万美元更高) 反事实观察 #3:Y 与 B 的价值为 500 万美元(因为 Y 有 400 万美元用于他观察到的最大合同,而 B 有 500 万美元,500 万美元更高)

这是描述我的数据的 Stata 示例代码..

clear
input str3(supplier buyer) float cont_value
"X" "A" 2
"X" "B" 5
"Y" "C" 4
end

除了 Statalist 上的答案 merge 是一系列数据集之外,您还可以使用 fillin 来实现。

clear *
input str3(supplier buyer) float cont_value
"X" "A" 2
"X" "B" 5
"Y" "C" 4
end

fillin supplier buyer
list 

tempvar cont_val cont_val1
by supplier, sort : egen `cont_val' = max(cont_value) 
by buyer, sort : egen `cont_val1' = max(cont_value)

egen cont_val = rowmax(`cont_val' `cont_val1') if _fillin
replace cont_val = cont_value if !_fillin

drop _fillin
sort supplier buyer

list supplier buyer cont_value cont_val
clear
set more off

input str3(supplier buyer) value
"X" "A" 2
"X" "B" 5
"Y" "C" 4
end

list

fillin supplier buyer

list, sepby(supplier)

bysort supplier : egen maxsupp = max(value) 
bysort buyer : egen maxbuy = max(value)

egen value2 = rowmax(max*) if _fillin
replace value2 = value if !_fillin

sort supplier buyer
order value2, after(value)
list, sepby(supplier)