楼主: liu022
1890 4

[原创博文] 采用Hash表计算配对样本的平方差距离 [推广有奖]

  • 0关注
  • 6粉丝

已卖:181份资源

讲师

64%

还不是VIP/贵宾

-

威望
0
论坛币
879 个
通用积分
7.4013
学术水平
23 点
热心指数
18 点
信用等级
21 点
经验
3667 点
帖子
430
精华
1
在线时间
375 小时
注册时间
2007-5-21
最后登录
2025-10-22

楼主
liu022 发表于 2012-9-30 22:58:28 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
大家好!
       小弟碰到一个大量数据查询的问题如下:
      数据集如下:
      stkcd    year     month     date    return
          1     2000         1            1        0.01
          1     2000         1            2        0.02
        .......
          2     2000         1            1        -0.01
          2     2000         1            2        0
        .......
     600999    2012      9          27      0.01
     600999    2012      9          28       0.10

      这个数据集转换为SAS格式大约200M

我需要算每个月,每两只股票收益率之差的平方和。例如2000年1月,要计算第一支股票1和其他股票对应日期收益率之差的平方和。第2只股票同理。。。。


       因为股票数量和日期都很长,所以按照通常的写法,用SQL方法来做的话,计算量很大很大,速度很慢。

       我知道Hash表可以用来避免排序直接进行查询等操作,请问各位老师能否用Hash表的方法来完成我的目标?如果可以,应该怎么完成?
二维码

扫码加我 拉你入群

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

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

关键词:配对样本 HASH Has RETURN 股票收益率 return 收益率 样本

沙发
yuerqieqie 发表于 2012-10-4 23:54:52
data test;
        input stkcd year month date return;
        datalines;
1 2000 1 1 0.01
1 2000 1 2 0.02
1 2000 1 3 0.01
2 2000 1 1 -0.01
2 2000 1 2 0
2 2000 1 3 0.01
3 2000 1 1 -0.02
3 2000 1 2 -0.01
3 2000 1 3 0
;
data result(keep = year month date stkcd return comp_stkcd comp_return result);
        retain year month date stkcd return comp_stkcd comp_return result;
        if _N_= 1 then do;
                declare hash h(dataset: "test", ordered: 'no');
                declare hiter iter('h');
                h.defineKey('year', 'month', 'date', 'stkcd');
                h.defineData('stkcd', 'return', 'year', 'month', 'date');
                call missing(stkcd, return, year, month, date);
                h.defineDone();
        end;

        set test(rename = (stkcd = comp_stkcd return = comp_return year = comp_year month = comp_month date = comp_date));
        /* Iterate over the hash */
        rc = iter.first();
        do while (rc = 0);
                if (year = comp_year and month = comp_month and date = comp_date and stkcd ne comp_stkcd) then do;
                        result = (return - comp_return)**2;
                        output;
                end;
                rc = iter.next();
        end;
run;
这样计算出两两股票在同一天的差的平方

藤椅
freeQQ 发表于 2012-10-5 00:14:14
正解,同意楼上!!!

板凳
liu022 发表于 2012-10-7 13:18:52
yuerqieqie 发表于 2012-10-4 23:54
data test;
        input stkcd year month date return;
        datalines;
谢谢老师指导~ call missing这句话是什么意思呢?因为我是Hash这方面的新手,还会问很多比较弱的问题~

报纸
yuerqieqie 发表于 2012-10-9 01:59:39
liu022 发表于 2012-10-7 13:18
谢谢老师指导~ call missing这句话是什么意思呢?因为我是Hash这方面的新手,还会问很多比较弱的问题~
我也是初学者,依葫芦画瓢写的程序。
关于call missing的官方解释是
The SAS compiler does not see variable assignments in the hash iterator. If there is no explicit assignment
statement for the variables specified in the DEFINEKEY and DEFINEDATA methods in the program itself, a
NOTE is written to the log that the variables are not initialized. CALL MISSING is used to avoid these notes.

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

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