标题: 请教一个LOO编程问题 [打印本页] 作者: shiyiming 时间: 2004-3-3 13:33 标题: 请教一个LOO编程问题 本人要对一模型(或称一种建模方法)进行LOO(leave one out)检验,具体如下:
原始数据有d1-d7,exp这8个变量,30组数据存放在一sas数据集中,每次剔除一组数据,用余下的29组数据建模(例如用d1-d7这7个变量对exp线性回归),然后用剔除的那组数据进行预测,算出差的平方;再剔除另外一组数据用余下29组建模,同样计算差的平方,如此下去将每组数据剔除一次,最后计算平方和。
我想问的是,如何编程实现将整个(30组)数据集剔除一组记录后的29组数据写入新的数据集中。
多谢!作者: shiyiming 时间: 2004-3-4 03:52
I will not go into your specific work, but I wrote the following simple example which you can follow to accomplish your task.
data a;
do i=1 to 30;
x=normal(1);
output;
end;
run;
%macro iter;
proc datasets;
delete result;
run;
%do i=1 %to 30;
data temp1 temp2;
set a;
if _n_=&i then output temp2;
else output temp1;
run;
proc means data=temp1 noprint;
var x;
output out=temp3 mean=mean;
run;
proc append base=result data=temp3;
run;
%end;
%mend;
%iter;作者: shiyiming 时间: 2004-3-4 14:08
多谢!我对宏不熟悉,但你的程序能解决我得问题!
附,整理后的程序:
%macro iter;
proc datasets;
delete result;
run;
%do i=1 %to 51;
data temp1 temp2;
set qsar.benzod_normal;
if _n_=&i then output temp2;
else output temp1;
run;
proc reg data=temp1 outest=temp(keep=nor_d1-nor_d7 intercept) noprint;
var _all_;
model nor_exp=nor_d1-nor_d7/selection=stepwise;
run;
proc iml;
use temp;
read var {nor_d1 nor_d2 nor_d3 nor_d4 nor_d5 nor_d6 nor_d7} into paravector;
read var {intercept} into intervalue;
use temp2;
read var {nor_d1 nor_d2 nor_d3 nor_d4 nor_d5 nor_d6 nor_d7} into onedata;
read var {nor_exp} into oneexp;
close;
do i=1 to 7;
if paravector[i]=. then paravector[i]=0;
end;
if intervalue=. then intervalue=0;
ds=(oneexp-onedata*paravector`-intervalue)##2;
create temp3 from ds;
append from ds;
run;
proc append base=result data=temp3;
run;
%end;
%mend;
%iter;作者: shiyiming 时间: 2004-3-4 22:16
The basic rule is that if you want to run a PROC within a DO loop, you have to write the SAS program in Macro. A PROC automatically breaks a DO loop in SAS data step, so the data step DO loop will never be complete. Macro DO loop works only within a Macro program.作者: shiyiming 时间: 2004-3-4 23:22
i see.your answer is very good.