您可以尝试使用Stata的egen命令中的total函数来实现有条件的除法。具体地,您可以按照以下步骤进行操作:
用sum命令计算世界的总值,比如命名为world_total:
sum value if commoditycode==000001
local world_total = r(sum)
用egen命令计算出每个国家每个产品代码的乘积,比如命名为product_total:
egen product_total = prod(value) if by(countrycode commoditycode)
用生成变量命令gen计算有条件的除法,即将每个国家每个产品代码的乘积除以世界的总值,比如命名为intensity:
gen intensity = product_total/world_total if commoditycode==000001
注意,这里的if条件需要和之前的sum命令中的条件一致,即commoditycode==000001,而不是010210。如果您想计算其他产品代码的值,只需要相应地更改这个条件即可。
另外,您在尝试用gen命令实现有条件的除法时出现了type mismatch的报错,可能是因为您的乘积变量product_total的数据类型不是数值型。您可以尝试用tostring命令将其转化为字符串型,然后再用egen命令计算出每个国家每个产品代码的字符串连接结果,最后用generate命令将其转化为数值型,并计算有条件的除法。具体操作如下:
tostring value, replace
egen product_str = concat(value) if by(countrycode commoditycode)
gen product_total = real(product_str)
gen intensity = product_total/world_total if commoditycode==000001