楼主: burnpark
4852 4

[原创博文] sas 累加 [推广有奖]

  • 1关注
  • 2粉丝

已卖:1份资源

讲师

21%

还不是VIP/贵宾

-

威望
0
论坛币
2979 个
通用积分
6.6000
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
4270 点
帖子
128
精华
0
在线时间
591 小时
注册时间
2009-3-17
最后登录
2024-10-13

楼主
burnpark 发表于 2009-11-23 15:58:52 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
我要将每只股票的3年累加起来,从而计算每只股票的3年的累计收益率。
请问在SAS中应该怎么编写这个程序?
比如有year id x,要y等于
year  id x y
1999 1  2 .
2000 1  3 .
2001 1  4 (2+3+4)
2002 1  2 (3+4+2)
2003 1  2 (4+2+2)
1990 2  3 .
1991 2  4 .
1992 2  3 (3+4+3)
1993 2  3 (4+3+3)
1994 2  2 (3+3+2)
1995 2  4 (3+2+4)
1996 2  3 (2+4+3)
...
二维码

扫码加我 拉你入群

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

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

关键词:year ear 计收益 收益率 程序 收益率

回帖推荐

sushe1527 发表于4楼  查看完整内容

data a;input year id x ; y=x+lag(x)+lag2(x); cards; 1999 1 2 2000 1 3 2001 1 4 2002 1 2 2003 1 2 1990 2 3 1991 2 4 1992 2 3 1993 2 3 1994 2 2 1995 2 4 1996 2 3 ;run; data b;set a; by id year;if first.id then m=0;m+1; if m

bobguy 发表于5楼  查看完整内容

This is better and clear. HTH data a; input year id x ; cards; 1999 1 2 2000 1 3 2001 1 4 2002 1 2 2003 1 2 1990 2 3 1991 2 4 1992 2 3 1993 2 3 1994 2 2 1995 2 4 1996 2 3 ; data b; set a; by id year; if first.id then m=0; m+1; x1=lag(x); x2=lag2(x); if m>=3 then y=sum(x, x1, x2); else y=.; drop m; run; proc print; run;

本帖被以下文库推荐

沙发
fancunhui 发表于 2009-11-23 16:25:00
可以这样做,首先产生虚的变量(我是指在计算过程中生产,但是不需要保留的变量,然后用条件替换的办法解决。
例如
y   id  x   dy             y=2+3+4 iff year=2001
         2  2+3+4      
         3  2+3+4
         4  2+3+4    2+3+4

藤椅
fancunhui 发表于 2009-11-23 16:25:43
其他的依次论推。当然应该建立循环程序,我想应该是很熟练的了吧。

板凳
sushe1527 发表于 2009-11-23 22:59:19
data a;input year  id x ;
y=x+lag(x)+lag2(x);
cards;
1999 1  2
2000 1  3
2001 1  4
2002 1  2
2003 1  2
1990 2  3
1991 2  4
1992 2  3
1993 2  3
1994 2  2
1995 2  4
1996 2  3
;run;

data b;set a; by id year;if first.id then m=0;m+1;
if m<=2 then y=.;drop m;run;
已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

报纸
bobguy 发表于 2009-11-25 22:27:07
This is better and clear.

HTH

data a;
input year  id x ;
cards;
1999 1  2
2000 1  3
2001 1  4
2002 1  2
2003 1  2
1990 2  3
1991 2  4
1992 2  3
1993 2  3
1994 2  2
1995 2  4
1996 2  3
;

data b;
   set a;
   by id year;
   if first.id then m=0;
   m+1;
   x1=lag(x);
   x2=lag2(x);
   if m>=3  then y=sum(x, x1, x2);
   else y=.;
drop m;
run;

proc print; run;
已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-30 22:27