You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Stata/SAS中构建经济体Dij映射及交互矩阵?

解决方案:Stata与SAS实现Dij变量生成

核心思路

你的数据集是宽格式:每行对应一个东道国(Host)-年份组合,列是该东道国对其他接收国(Recipient)的贸易值。首先需要将宽格式转换为长格式,得到每个Host-Recipient-时间的独立观测,再生成Dij变量(Host与Recipient编号不同时为1,相同时为0)。


Stata 实现代码

* 1. 重命名Host的国家代码列,方便后续处理
rename 国家代码 Host_code

* 2. 将宽格式转长格式,提取所有Host-Recipient-时间的贸易观测
reshape long , i(Host Host_code 时间) j(Recipient_code) string

* 3. 创建国家代码与编号的映射表
preserve
keep Host Host_code
duplicates drop
rename Host Recipient
rename Host_code Recipient_code
tempfile country_map
save `country_map'
restore

* 4. 匹配Recipient的编号
merge m:1 Recipient_code using `country_map'
drop _merge

* 5. 生成Dij变量
gen Dij = (Host != Recipient)

SAS 实现代码

/* 1. 创建国家代码与编号的映射数据集 */
data country_map;
    set your_dataset; /* 替换为你的数据集名称 */
    keep 国家代码 Host;
    rename 国家代码=Recipient_code Host=Recipient;
    if _n_=1 then do;
        declare hash h();
        h.definekey('Recipient_code');
        h.definedata('Recipient');
        h.definedone();
    end;
    if h.find() ne 0 then h.add();
run;

/* 2. 将宽格式转长格式,拆分贸易列 */
data long_data;
    set your_dataset; /* 替换为你的数据集名称 */
    length Recipient_code $3;
    array trade_cols[*] AUS: BEL: CAN:; /* 用通配符匹配所有贸易列,可根据实际列名调整 */
    do i=1 to dim(trade_cols);
        Recipient_code = vname(trade_cols[i]);
        trade_value = trade_cols[i];
        output;
    end;
    drop i AUS: BEL: CAN:; /* 删除原宽格式的贸易列 */
run;

/* 3. 匹配Recipient编号并生成Dij变量 */
data final_data;
    merge long_data country_map;
    by Recipient_code;
    Dij = (Host ne Recipient);
run;

内容的提问来源于stack exchange,提问作者Amiti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 23:40:26