/* 工作半年都没有接触IML,研究生时候用过IML */
/* 代码写的比较不专业,我仍觉得IML比较强大 */
/* 完全可以脱离macro,IML高手都很低调*/
/* 没有IML权限,没法调试,有误的可能性大*/
data a1;
input date $ ticker $ return smb hml;
cards;
19930104 AC 0.0298 -0.003 0.0023
19930105 AC 0.0334 0.0037 0.0026
19930106 AC 0.0432 0.0041 0.0025
19930107 AC 0.0256 0.0066 0.0017
19930108 AC 0.0355 0.0002 -0.0019
19930109 AC 0.056 0 0.003
19930110 AC 0.025 -0.0029 0.0018
19930111 AC 0.0345 0.001 0.009
19930112 AC 0.0435 0.0019 0.003
19930104 BDF 0.0435 0.0019 0.0018
19930105 BDF 0.0298 0.0016 0.009
19930106 BDF 0.0334 -0.0005 0.003
19930107 BDF 0.0432 0.0025 0.0003
19930108 BDF 0.0256 0.0062 0.0027
19930109 BDF 0.0355 -0.0019 0.0025
19930110 BDF 0.056 0.002 0.0026
19930111 BDF 0.025 -0.003 0.0023
19930112 BDF 0.0345 0.0037 0.0026
;
run;
/*根据ticket读到数据集,IML也可以实现,IML不熟悉,忘了怎么处理*/
proc sql;
create table t1 as
select distinct ticker
from a1;
quit;
proc sql noprint;
select count(*) into: numrows
from t1;
%let nn=&numrows;
select ticker into: ticker1- :ticker&nn
from t1;
quit;
%macro one;
%do i = 1 %to &nn;
data b&i;
set a1;
where ticker = "&&ticker&i";
run;
%end;
%mend;
%one;
%macro two;
proc iml;
/*自定义回归模块,X为自变量矩阵,y为因变量向量*/
start reg(x,y);
n=nrow(x); /* number of observations */
k=ncol(x); /* number of variables */
xpx=x`*x; /* cross-products */
xpy=x`*y;
xpxi=inv(xpx); /* inverse crossproducts */
b=xpxi*xpy; /* parameter estimates */
yhat=x*b; /* predicted values */
resid=y-yhat; /* residuals */
sse=resid`*resid; /* sum of squared errors */
dfe=n-k; /* degrees of freedom error */
mse=sse/dfe; /* mean squared error */
rmse=sqrt(mse); /* root mean squared error */
covb=xpxi#mse; /* covariance of estimates */
stdb=sqrt(vecdiag(covb)); /* standard errors */
t=b/stdb; /* ttest for estimates=0 */
probt=1-probf(t#t,1,dfe); /* significance probability */
finish reg;
free re;
%do i= 1 %to &nn;
proc iml;
use b&i;read all var _num_ into c;
call reg(C[,2:3],c[,1]);
re=re//(b||RESID||sse||t||probt); /*保存每次回归的结果R-sqare可以公式自己在REGmodule加上去,*/
%end;
create table result from re;append from re;
quit;
%mend;
%two;
|