简体   繁体   English

使用SAS计算一段时间内的收益

[英]Calculate a return for a period of time using SAS

My data is a set of columns as follows: 我的数据是一组列,如下所示:

gvkey   date        div    price
1166    19970429    0   25.6
1166    19970502    0   26
1166    19970505    0   25.9
1166    19970506    0   22.1
1166    19970507    0   19
1166    19970509    0   20.4
1166    19970512    0   21.4
1166    19970513    0   21.7
1166    19970514    0   21.7
1166    19970515    0   21.1
1166    19970516    0   21.5
1166    19970520    0   21
1166    19970521    0   21.8
1166    19970522    0   22.2
1166    19970523    0   22.7
1166    19970526    0   23.9
1166    19970527    0   24
1166    19970528    0   24.2
1166    19970529    0   24.3
1166    19970530    0   23.7   

In excel, I was able to calculate the return for the month of May just by adding a column that calculated the daily rtn ((price + div)/lag price)). 在excel中,我只需添加一栏即可计算出每日rtn((价格+ div)/滞后价格),就可以计算出5月份的收益。 Then I add a column of one plus rtn which multiplies today's daily rtn by the prior row one plus value. 然后,我添加一列+1加rtn,该列将今天的每日rtn乘以前一行的加号。 Of course, for the first day of the period, the one plus rtn value = daily rtn. 当然,对于该期间的第一天,一个加rtn值=每日rtn。

In excel I calculated 0.925781 as the one plus rtn for May 30th and -0.07422 as the rtn for that month. 在excel中,我将0.925781作为5月30日的rtn加-0.07422作​​为该月的rtn。 How can I make SAS do this for me? 我该如何使SAS为我做到这一点? Thanks for your help! 谢谢你的帮助!

Try this: 尝试这个:

data want;
set have;
format rtn crtn percent12.4;
retain crtn 1;
rtn = (price + div)/lag(price) - 1;
crtn = (1 + crtn) * (1 + sum(rtn,0)) - 1;
run;

This calculates return rtn as you describe. 如您所描述的那样,这将计算返回rtn

We create a cumulative return crtn with a starting value of 1. RETAIN tells SAS to keep the value of this variable between rows. 我们创建了一个累计回报crtn为1。初始值RETAIN告诉SAS保留行之间的这个变量的值。 So we then update it on each row. 因此,我们随后在每一行上对其进行更新。

sum(rtn,0) = rtn + 0 . sum(rtn,0) = rtn + 0 On the first row, RTN will be null, but the SUM() function will ignore that null and return 0. 在第一行中,RTN将为null,但SUM()函数将忽略该null并返回0。

EDIT: Ask in comments about having the cumulative return reset each year. 编辑:询问有关每年重置累计收益的评论。 This assumes a variable fyear is on the data set and the data continue to be sorted in the correct order. 假定变量fyear在数据集上,并且数据继续以正确的顺序排序。

data want;
set have;

by fyear;

format rtn crtn percent12.4;
retain crtn 1;

if first.fyear then do; /*Check for the start of a new BY group*/
    crtn = 1;
end;

rtn = (price + div)/lag(price) - 1;
crtn = (1 + crtn) * (1 + sum(rtn,0)) - 1;
run;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM