%let n=9;
sasfile a load;
data rest0;
set a;
by id;
array _x{4} _temporary_;
array _a{2,&n} _temporary_;
if first.id then do;
do i=1 to dim(_x); _x[i]=0; end;
do i=1 to dim(_a, 1);
do j=1 to dim(_a, 2);
_a[i, j]=0;
end;
end;
end;
m=mod(num-1, &n)+1;
_a[1, m]=x; _a[2,m]=y;
link filler;
m2=mod(num-&n, &n)+1;
if num>=&n then do;
if num>&n then do;
link deduct;
end;
beta=(_x[2]-_x[1]*_x[4]/&n)/(_x[3]-_x[1]**2/&n);
intercept=_x[4]/&n - beta*_x[1]/&n;
keep id num intercept beta ;
output;
end;
return;
filler:
_x[1]+x;
_x[2]+x*y;
_x[3]+x**2;
_x[4]+y;
return;
deduct:
_x[1]=_x[1]-_a[1,m2];
_x[2]=_x[2]-_a[1,m2]*_a[2,m2];
_x[3]=_x[3]-_a[1,m2]**2;
_x[4]=_x[4]-_a[2,m2];
return;
run;
sasfile a close;
/* double check with standard approach */
proc reg data=a;
where id=001 and num<10;
model y =x;
run;quit;
[/code:44khth0f]作者: shiyiming 时间: 2011-9-2 14:42 标题: Re: 请问一个循环进行回归的问题,谢谢! 速度超快!
%let n=10;
sasfile a load;
data rest0;
set a;
by id;
array _x{4} _temporary_;
array _a{2,&n} _temporary_;
if first.id then do;
do i=1 to dim(_x); _x[i]=0; end;
do i=1 to dim(_a, 1);
do j=1 to dim(_a, 2);
_a[i, j]=0;
end;
end;
end;
m=mod(num-1, &n)+1;
if num<=&n then do;
_a[1, m]=x; _a[2,m]=y;
link filler;
end;
if num>=&n then do;
if num>&n then do;
link deduct;
_a[1, m]=x; _a[2,m]=y;
link filler;
end;
beta=(_x[2]-_x[1]*_x[4]/&n)/(_x[3]-_x[1]**2/&n);
intercept=_x[4]/&n - beta*_x[1]/&n;
keep id num intercept beta ;
output;
end;
return;
filler:
_x[1]+x;
_x[2]+x*y;
_x[3]+x**2;
_x[4]+y;
return;
deduct:
_x[1]=_x[1]-_a[1,m];
_x[2]=_x[2]-_a[1,m]*_a[2,m];
_x[3]=_x[3]-_a[1,m]**2;
_x[4]=_x[4]-_a[2,m];
return;
run;
sasfile a close;