全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
1869 4
2012-09-30
大家好!
       小弟碰到一个大量数据查询的问题如下:
      数据集如下:
      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表的方法来完成我的目标?如果可以,应该怎么完成?
二维码

扫码加我 拉你入群

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

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

全部回复
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;
这样计算出两两股票在同一天的差的平方
二维码

扫码加我 拉你入群

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

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

2012-10-5 00:14:14
正解,同意楼上!!!
二维码

扫码加我 拉你入群

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

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

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

扫码加我 拉你入群

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

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

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.

二维码

扫码加我 拉你入群

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

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

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群