生成演示数据,Stata代码如下:
clear
input byte id str6 province str3 short
1 "上海" "沪"
2 "江苏" "江"
3 "浙江" "浙"
4 "北京" "京"
5 "天津" "津"
6 "河北" "冀"
end
批量的话,可以用循环,Stata代码如下:
foreach var of varlist province short {
encode `var', gen(`var'_id)
}
list
list, nolabel
批量循环,还可以用egen的group函数,Stata代码如下:
foreach var of varlist province short {
egen `var'_id1=group(`var')
}
list
list, nolabel
此外,还可以用Stata社区命令multencode,Stata代码如下:
*安装Stata社区命令
ssc install multencode, replace
multencode province short, gen(province_id2 short_id2)
list
list, nolabel