全部版块 我的主页
论坛 计量经济学与统计论坛 五区 计量经济学与统计软件 Stata专版
1395 4
2014-01-15
受到警告
悬赏 200 个论坛币 未解决
请教各位同学,假设数据如下

2001                             Bank of America
2001                             General Motors
2001                             General Elec
2002                             General Motors
2002                             General Elec
2003                             Goldman Sachs
2003                             General Elec

第一列是时间,第二列是当年的记录,每年记录数不同
现在我想检查每一年的各项记录是不是在上一年出现过,生成如下图第三列一样的新变量,该如何写呢?

2001                             Bank of America               
2001                             General Motors                  
2001                             General Elec                     
2002                             General Motors                1
2002                             General Elec                    1
2003                             Goldman Sachs               0
2003                             General Elec                    1

多谢了

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2014-1-15 06:45:51
sort by the 2nd column and compare using current row to previous row?

sort v2
gen v3=1 if v2==v2[_n-1] & v1==v1[_n-1]+1
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2014-1-15 21:41:26
我想想。。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2014-1-15 22:05:22
Assume that each company only appear once each year, and in the following, even try to make your data a little more complex (there are gaps between years)

clear
input year str20 comp
2001  "Bank of America"
2001  "General Motors"
2001  "General Elec"
2002  "General Motors"
2002  "General Elec"
2003  "Goldman Sachs"
2003  "General Elec"
2008  "General Motors"
2009  "Goldman Sachs"
end

bysort comp (year): gen num=year[_n]-year[_n-1]
replace num=0 if num>1
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2014-1-19 12:00:52
sort year name /*如果数据天然排列齐整,此步可略*/
egen t=tag(name)

*t=1表示不重复,t=0表示重复。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群