楼主: playmore
1851 8

[问答] 请问利用每条观测的多个变量进行矩阵计算的问题 [推广有奖]

已卖:1645份资源

学科带头人

2%

还不是VIP/贵宾

-

TA的文库  其他...

R相关

经济学相关

金融工程

威望
1
论坛币
16356 个
通用积分
8.6697
学术水平
372 点
热心指数
394 点
信用等级
341 点
经验
15297 点
帖子
1194
精华
1
在线时间
1332 小时
注册时间
2007-1-11
最后登录
2025-12-1

初级学术勋章 初级热心勋章 中级热心勋章

楼主
playmore 发表于 2013-5-3 18:53:09 |AI写论文
200论坛币
我现在有如下的表格:

Stk_Code End_Date Factor1 Factor2 ... FactorN
600519     2012/4/1    0.2      0.8     ....    1.4

每条观测是某支股票,在某一天的N个因子,N在几十的量级,比较大
针对这每条观测,我都想利用这N个因子,和其他给定的矩阵,进行矩阵运算,然后得到几个返回值,都是数值,非向量或矩阵
因此我想在计算完后,再把这几个返回值补回这个表格

不知道用什么办法可以解决这个问题,我想到可以把矩阵的算法写成宏,然后在Data步里用call execute调用
但是有如下问题:
第一,好像SAS的宏只能有一个返回值,我需要多个;
第二,我的宏参数比较多(N个因子),用call execute调用宏的时候会比较麻烦,我也不知道可不可以用临时数组作为宏的参数传进去

另外可行的办法是直接把整个表传到一个矩阵里,然后全部运算用Proc iml进行
但是在Proc iml的矩阵就没法把Stk_Code和End_Date这样的变量传进去做为By变量了(除非把这两个变量全部作为数值考虑)
而且这个表非常大,几十万行,全部整到一个矩阵里我还没有试过

希望高手解答
另我最近在研究R,如果R有好的解决办法,也请不吝赐教,谢谢!
   

最佳答案

邓贵大 查看完整内容

PROC FCMP支持有限的矩阵运算 先生成一个函数 然后在DATA STEP里调用上面的函数得到你的参数 options cmplib=work.funcs; data giglo; set stock_data; array factors[&n] factor1-factorN; array _x[&n] _temporary_; do _n_=1 to dim(_x); _x[_n_] = factors[_n_]; end; call dude(_x, retval1, retval2); run; 因为你的算法没给出来,所以不能写得更具体,但大致如此! Documentation: http://support.sas.com/do ...
关键词:多个变量 EXECUTE factor Facto data步 计算

本帖被以下文库推荐

playmore邀请您访问ChinaTeX论坛!!!进入ChinaTeX论坛

沙发
邓贵大 发表于 2013-5-3 18:53:10
PROC FCMP支持有限的矩阵运算
先生成一个函数
  1. proc fcmp outlib=work.funcs.test;
  2. subroutine dude(x[*], retval1, retval2);
  3. outargs retval1 retval2;
  4. *your calculations here;
  5. endsub;
  6. run;
复制代码
然后在DATA STEP里调用上面的函数得到你的参数
options cmplib=work.funcs;
data giglo;
set stock_data;
array factors[&n] factor1-factorN;
array _x[&n] _temporary_;
do _n_=1 to dim(_x);
_x[_n_] = factors[_n_];
end;
call dude(_x, retval1, retval2);
run;
因为你的算法没给出来,所以不能写得更具体,但大致如此!
Documentation: http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a002890483.htm
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
playmore + 1 + 1 + 1 精彩帖子

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

Be still, my soul: the hour is hastening on
When we shall be forever with the Lord.
When disappointment, grief and fear are gone,
Sorrow forgot, love's purest joys restored.

藤椅
playmore 发表于 2013-5-3 21:23:26
邓贵大 发表于 2013-5-3 21:05
PROC FCMP支持有限的矩阵运算
先生成一个函数
然后在DATA STEP里调用上面的函数得到你的参数
嗯,proc fcmp是不错
我好好研究下,谢谢!
另问一下proc fcmp的使用是否像其他SAS过程一样,即不能proc嵌套
这样就不能在proc fcmp里调用proc iml以进行较复杂的矩阵运算了?
playmore邀请您访问ChinaTeX论坛!!!进入ChinaTeX论坛

板凳
邓贵大 发表于 2013-5-3 22:05:04
yes, you may invoke other PROCs from PROC FCMP using the RUN_MACRO trick, but then it'll probably run slower than PROC IML alone.
you got to do all your matrix arithmetics within the function declared in PROC FCMP, or you'd better do it in another way.
I cannot determine the feasibility of my FCMP approach unless I know exactly what you're calculating.
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
playmore + 1 + 1 + 1 观点有启发

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

Be still, my soul: the hour is hastening on
When we shall be forever with the Lord.
When disappointment, grief and fear are gone,
Sorrow forgot, love's purest joys restored.

报纸
playmore 发表于 2013-5-3 22:14:21
邓贵大 发表于 2013-5-3 22:05
yes, you may invoke other PROCs from PROC FCMP using the RUN_MACRO trick, but then it'll probably ru ...
多谢!刚才看了下proc fcmp的例子
这个run_macro真是十分tricky的用法

另外直接在proc fcmp里能完成多复杂的矩阵运算啊?
我这里也就是向量和矩阵的乘法、求个内积、取个模之类的
在proc iml里也就是最基础的运算
在proc fcmp里能用到的运算应该和data步里类似吧?也就是最多到向量级别(用array)?矩阵就不行了吧?
playmore邀请您访问ChinaTeX论坛!!!进入ChinaTeX论坛

地板
邓贵大 发表于 2013-5-3 22:21:36
playmore 发表于 2013-5-3 22:14
多谢!刚才看了下proc fcmp的例子
这个run_macro真是十分tricky的用法
It's doable
http://support.sas.com/documenta ... .htm#a003193719.htm
Be still, my soul: the hour is hastening on
When we shall be forever with the Lord.
When disappointment, grief and fear are gone,
Sorrow forgot, love's purest joys restored.

7
Imasasor 发表于 2013-5-4 09:31:22
你不会的我也肯定不会,所以就不用问我了,并且现在我已经戒SAS了,专心灌科研的水
欢迎加入亚太地区第一R&Python数据挖掘群: 251548215;

8
playmore 发表于 2013-5-4 10:31:21
邓贵大 发表于 2013-5-3 22:21
It's doable
http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a00319 ...
嗯,多谢,看了下,还真是简陋的可以啊,呵呵
playmore邀请您访问ChinaTeX论坛!!!进入ChinaTeX论坛

9
zjwdjcz 发表于 2013-5-28 11:48:56
好東西

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

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