楼主: yllbyl
2431 7

求高手帮忙优化做回归的宏程序,可以正常运行,但速度会越来越慢。希望能快点! [推广有奖]

  • 0关注
  • 0粉丝

已卖:15份资源

大专生

23%

还不是VIP/贵宾

-

威望
0
论坛币
41 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
519 点
帖子
36
精华
0
在线时间
54 小时
注册时间
2010-1-4
最后登录
2020-7-22

楼主
yllbyl 发表于 2013-5-17 20:58:48 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
编程的目的是:将每个股票(300多个)从第1个交易日(不同股票不一样)到第n个交易日(n=10~1200)的股票价格看做y变量,将大盘指数看做x变量,做回归,得出自变量的系数,系数的p值,回归的拟合优度。并将这些结果以及每次计算的时间、股票代码做成表格的形式呈现出来。
我已经编好了,但速度比较慢,而且越运行到后面就越慢,但是关闭SAS重新开始,就快些了。
一共要做240000多次回归,如果按照这速度。。。1年才能做完啊。。。希望高手帮忙优化!争取答辩之前做出来呀!

%macro yy(code,date);
/*创建一个新数据集,用于存放从原始数据beta中提取的需要回归的数据*/
data data&code&date;
set beta;
where code=&code and date<=&date;

/*将回归结果显示到数据集中*/
ods listing close;
ods output parameterestimates(match_all persist=proc)=Estimates&code&date;
ods output fitstatistics(match_all persist=proc)=fitstatistics&code&date;

/*做回归*/
proc reg data=data&code&date;
model prc=idx ;
run;

/*对目标数据集中加入两个变量:时间和股票代码*/
data Estimates&code&date;
set Estimates&code&date;
code=&code;
date=&date;
run;

data fitstatistics&code&date;
set fitstatistics&code&date;
code=&code;
date=&date;
run;

/*删除数据集中多余的变量,并保存到optimize文件夹中*/
data optimize.Est;
set Estimates&code&date optimize.Est;
drop _Run_ Model DF _proc_ dependent;
run;
data optimize.fits;
set fitstatistics&code&date optimize.fits;
drop _Proc_ _Run_ Model Label1 cValue1 nValue1 dependent;
run;
ods listing;

/*删除所有的临时数据集*/
proc datasets library=work kill nolist; quit;

   %mend yy;
%include "E:\optimize\yy2.txt";
run;
quit;



yy2.txt:(大概内容如下)
%yy( 600603 , 19971201 );%yy( 600603 , 19971202 );%yy( 600603 , 19971203 );%yy( 600603 , 19971204 );%yy( 600603 , 19971205 );%yy( 600603 , 19971208 );%yy( 600603 , 19971209 );%yy( 600603 , 19971210 );%yy( 600603 , 19971211 );%yy( 600603 , 19971212 );%yy( 600603 , 19971215 );

二维码

扫码加我 拉你入群

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

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

关键词:宏程序 越来越 求高手 Macro Where 程序优化 回归

沙发
邓贵大 发表于 2013-5-17 21:03:39
BY-Group processing is the key, DO NOT use  macro at all.

(1) First repeat each record as many times as the number regressions including the record.
Sort your data by code and date.
Within code i: Let N_i = # of dates within Code i. Repeat record 1 for N_i-9 times, record 2 for N_i-10 times, ..., and so on.
Assign each replicate into a unique regression #.
(2) Sort by code, Regression #
(3) run PROC REG by code and regression #.
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.

藤椅
yllbyl 发表于 2013-5-17 21:19:57
邓贵大 发表于 2013-5-17 21:03
BY-Group processing is your answer.
group by语句吗?分组?能说具体一点吗?谢谢!

板凳
harlon1976 发表于 2013-5-17 21:29:22
邓贵大 发表于 2013-5-17 21:03
BY-Group processing is your answer.
能写出具体的程序吗,给大家介绍共同学习一下。

报纸
邓贵大 发表于 2013-5-17 22:03:14
It takes about 10-20 minutes.
  1. data hell;
  2.         do code =  1 to 300;
  3.                 do date = 1 to ceil(2400 * ranuni(0));
  4.                         prc = ranuni(12345);
  5.                         idx = ranuni(12345);
  6.                         output;
  7.                 end;
  8.         end;
  9.         
  10. run;

  11. data hell1;
  12.         do N=1 by 1 until(last.code);
  13.                 set hell;
  14.                 by code date;
  15.         end;
  16.         
  17.         do i=1 by 1 until(last.code);
  18.                 set hell;
  19.                 by code date;
  20.                
  21.                 do NPOINT=N to max(10, i) by -1;
  22.                         output;
  23.                 end;
  24.         end;
  25. proc sort data=hell1;
  26.         by code NPOINT;
  27. ods _all_ close;
  28. proc reg data=hell1;
  29.         by code NPOINT;
  30.         model prc = idx;
  31.         ods output parameterestimates = outest
  32.                 fitstatistics = outstat;
  33. run;
  34. ods listing;
复制代码
If you're pretty familar with the matrix algebra behind Simple Linear Regression, do it in IML or maybe PROC FCMP. That'll be much faster as you don't have to roll your data along the time line.
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.

地板
jingju11 发表于 2013-5-18 04:47:28
邓贵大 发表于 2013-5-17 22:03
It takes about 10-20 minutes. If you're pretty familar with the matrix algebra behind Simple Linear  ...
非常精彩的sas语句。受益良多。
对作者的要求也许略有不同。比如max(10,)。但是无论如何,10以下的数据是几乎不消耗时间的。
今天刚好下午清闲。按照作者的数据尺寸(24万的数据,每个code含有记录从10到1000不等),把这个ROLLING REGRESSION做了一遍。大概时间消耗如下:
展开数据:4分钟(数据记录大概1亿上下)
排序:13分钟
回归:5分钟
其他:0.5分钟

总计:22.5分钟。顺便说一句:我的电脑配置很烂。
因为是simple regression, (one predictor), 所以可以在数据步里做。只需要10秒左右。没有iml。无法测试。
另外,这也印证了我的一个感觉:凡是在SIMULATION里写宏程序的,基本上都不自觉地采用那个最慢的方法。
京剧
具体可以参看:http://blog.sina.com.cn/s/blog_a3a926360101cxve.html

7
bobguy 发表于 2013-5-18 05:20:47
"...一共要做240000多次回归"

I do not quite get it. Why do you have so many regressions?

8
邓贵大 发表于 2013-5-18 19:26:28
jingju11 发表于 2013-5-18 04:47
非常精彩的sas语句。受益良多。
对作者的要求也许略有不同。比如max(10,)。但是无论如何,10以下的数 ...
多谢惊惧大师点评,并拜读雄文,佩服你的总结能力!
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.

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-1 10:26