楼主: cris1988
2876 5

[编程问题求助] 计算过去特定一段时间某ID重复出现的次数,如果存在折现系数怎么在stata实现? [推广有奖]

  • 0关注
  • 0粉丝

本科生

47%

还不是VIP/贵宾

-

威望
0
论坛币
515 个
通用积分
2.5504
学术水平
0 点
热心指数
2 点
信用等级
0 点
经验
2567 点
帖子
28
精华
0
在线时间
145 小时
注册时间
2009-8-21
最后登录
2024-8-12

楼主
cris1988 发表于 2014-2-7 11:53:43 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
在此处借用了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编程,谢谢各位同仁!


二维码

扫码加我 拉你入群

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

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

关键词:Stata tata stata编程 thread pinggu

沙发
jjjj6666 发表于 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
  }
}
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
Sunknownay + 2 + 2 + 2 热心帮助其他会员

总评分: 学术水平 + 2  热心指数 + 2  信用等级 + 2   查看全部评分

藤椅
cris1988 发表于 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'?

板凳
jjjj6666 发表于 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.

报纸
cris1988 发表于 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为目标值的。

地板
jjjj6666 发表于 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.

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-3 22:40