Stata如何快捷地生成唯一识别符?

安装Stata社区命令makeid的Stata命令如下:

*ssc 托管
ssc install makeid, replace
* github托管
makeid from https://raw.githubusercontent.com/bbdaniels/stata/main
net describe makeid
net install makeid, replace

读取演示数据,Stata代码如下:

clear
input country village year household
 11        19        1960      1
 1        119        1960      1
111         9        1960      1
end

这个例子比较特殊,虽然country、village、year和household四个变量在一起是没有重复,判定的Stata命令如下:


isid country village year household

但直接拼接的话,这三条记录都是7位数字1119601,错误示范:


egen id = concat(country village year household)
list
isid id

我们可以将其转换为字符拼接,Stata代码如下:


gen id1 = string(country) + " " + string(village) + " " + string(year) + "" + string(household) 

我们还可以使用group命令,Stata代码如下:


egen id2 = group(country village year household)
list

我们也可以使用Stata社区命令makeid,Stata代码如下:


sysuse auto.dta , clear
* project选项是必选,唯一变量值的前缀为project的首字母,本例为L
makeid country village year household, gen(id3) project(Lian)
list