楼主: zenith1107
2217 1

[原创博文] SAS 股票价格变动计算问题 [推广有奖]

  • 0关注
  • 0粉丝

初中生

28%

还不是VIP/贵宾

-

威望
0
论坛币
4 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
100 点
帖子
8
精华
0
在线时间
16 小时
注册时间
2008-2-23
最后登录
2013-11-12

楼主
zenith1107 发表于 2010-5-25 03:03:42 |AI写论文
10论坛币
Hi,

I have a daily stock price database that is not complete (i.e. with gaps
between dates). I need a code that can, for each ticker and date in the
dataset, calculate the return between that date and N days prior to that.
(N=1, 3, 7, ...etc). I can't use the LAG function since the dates are not
continuous and I'm wondering if there's a way to somehow combine LAG and
INTCK. The dataset is as follows (suppose there are no weekends):

Ticker  Date        Price
ABC   1/1/2002  10
ABC   1/2/2002  11
ABC   1/5/2002  12  (NOTE THE 2-day GAP HERE!!!!!)
ABC   1/6/2002  13
ABC   1/7/2002  14
DEF    1/1/2002
.........................

and now let's say I want to calculate the 3-day return. I'd like SAS to
automatically calculate the number of days between the current date and pick
the correct reference date to calculate the return. LAG function cannot do
that. Specifically, I'd like the output to be

Ticker  Date       Return
ABC   1/1/2002  .
ABC   1/2/2002  .
ABC   1/5/2002  12/11-1 (CORRECT 3-day return)
ABC   1/6/2002  .
ABC   1/7/2002  .
DEF    1/1/2002 .
.........................

INSTEAD OF the one calculated with a LAG function:

Ticker  Date       Return
ABC   1/1/2002  .
ABC   1/2/2002  .
ABC   1/5/2002  12/10-1 (INCORRECT return)
ABC   1/6/2002  13/11-1
ABC   1/7/2002  14/12-1
DEF    1/1/2002 .
.........................

I know INTCK and INTNX is supposed to do the trick but I've been having a
hard time figuring out how to incorporate that into the lag calculations.

Any help is greatly appreciated!

关键词:股票价格 价格变动 计算问题 Calculations Calculation function database 股票价格 between somehow

沙发
mydo 发表于 2010-5-26 10:56:09
I hesitate to use LAG... I would try something like this:

data aaa;
input ticker $ date mmddyy10. price;
format date mmddyy10.;
cards;
ABC   1/1/2002  10
ABC   1/2/2002  11
ABC   1/5/2002  12
ABC   1/6/2002  13
ABC   1/7/2002  14
DEF   1/1/2002  9
DEF   1/2/2002  11
DEF   1/5/2002  13
DEF   1/6/2002  15
DEF   1/7/2002  18
;
run;
proc sort; by ticker date; run;

* assign n=1,3,5 etc,  n=1 means return between that date and 1 business day prior * ;
%let nday = 1;

data aaa1;
set aaa;
by ticker date;
if first.ticker then n = 1;
else n+1;
seq = mod(n,&nday);
run;
proc sort; by ticker seq date; run;

data bbb;
set aaa1;
by ticker seq date;
retain temp;
if first.seq then do; temp = price; return = .; end;
else do;
        return = price/temp - 1;
        temp = price;
end;
drop temp seq n;
run;
proc sort; by ticker date; run;
               
有点啰嗦,见谅哈!
已有 1 人评分热心指数 收起 理由
crackman + 1 精彩帖子

总评分: 热心指数 + 1   查看全部评分

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

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