全部版块 我的主页
论坛 计量经济学与统计论坛 五区 计量经济学与统计软件 Stata专版
2827 5
2014-02-07
在此处借用了xingxf同学的数据,特此感谢!原帖https://bbs.pinggu.org/thread-2176491-1-1.html

有如下格式数据:
ID    Date                   Var1
AA   1980/01/02
B     1980/01/31
C     1982/02/16
D     1982/03/18
AA    1982/03/21
AA    1982/03/21
B      1982/03/22
AA    1983/06/30
E      1985/11/22
AA    1985/12/31
G      1987/01/01
D      1991/02/27
.
AA    1999/03/16
.
Z     2010/12/31

在Var1列生成本行对应ID过去5年重复出现的次数(不计当次),但是需要按相差年数折旧(也是不同于xingxf同学原帖的地方)。例如,对于AA    1983/06/30 来说,AA在1980/01/02,1982/03/21,1982/03/21各出现一次,
那么Var1=1/(1983-1980)+1/(1983-1982)+1/(1983-1982)

请问如何用stata编程,谢谢各位同仁!


二维码

扫码加我 拉你入群

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

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

全部回复
2014-2-9 07:41:28
may not be the best way, try this
*generate year
gen year=year(date(date,"YMD"))
sort id year
by id : gen y=_n
su y
local max=r(max)
gen var1 = .
replace var1 = 0 if y>1
forv i=`max'(-1)2 {
  local j1=`i'-1
  local j2=max(`i'-5,1)
  local jj=0
  forv j=`j1'(-1)`j2' {
    local jj=`jj'+1
    replace var1=var1+1/(year-year[_n-`jj']) if y==`i' & year-year[_n-`jj'] != 0
  }
}
二维码

扫码加我 拉你入群

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

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

2014-2-11 10:50:50
jjjj6666 发表于 2014-2-9 07:41
may not be the best way, try this
*generate year
gen year=year(date(date,"YMD"))
谢谢回复,不明白最后一行命令为什么要加y==`i'?
二维码

扫码加我 拉你入群

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

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

2014-2-11 10:59:28
the reason is that we want to sum over previous years' (up to 5) from this year (target), so using y==`i' for that reason.
二维码

扫码加我 拉你入群

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

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

2014-2-11 11:16:14
jjjj6666 发表于 2014-2-11 10:59
the reason is that we want to sum over previous years' (up to 5) from this year (target), so using y ...
不好意思,多问您的一个问题,命令里是怎么确保计算的是同一ID的出现次数的啊?似乎是以y为目标值的。
二维码

扫码加我 拉你入群

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

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

2014-2-12 06:10:55
the for loop inside:

forv i=`max'(-1)2 {
...
}

will loop within the same id only because the data is sorted by id and we have y==`i' to make sure the sum is over the same id.
二维码

扫码加我 拉你入群

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

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

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

说点什么

分享

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