我想对一个数据集作如下处理:给满足条件的某些观测前后一定范围的观测作出标记。
这里我用了IML处理,主要代码如下:
proc iml;
use b;
read all var _all_ into x;
do i=1 to nrow(x);
if x[i,3] = 1
then
do while(i-50<=j<=i+50);
x[j,6] = 1;
end;
end;
create new from x;
append from x;
quit;
但是日志却显示有错误:
ERROR: (execution) Matrix has not been set to a value.
operation : <= at line 336 column 18
operands : _TEM1003, J
_TEM1003 1 row 1 col (numeric)
140
J 0 row 0 col (type ?, size 0)
statement : DO at line 336 column 5
ERROR: Error evaluating DO WHILE expression; DO loop exited.
proc iml;
use b;
read all var _all_ into x;
do i=1 to nrow(x);
if x[i,3] = 1 then do;
do j=-50 to 50;
if ((j-50>=1) & (j+50<=nrow(x) )) then do;
x[j,6] = 1;
end;
end; *do j;
end; *if ;
end; *i;
create new from x;
append from x;
quit;
--> while(i-50<=j<=i+50); 這不是程序, J 沒有定義, 且二個條件寫在一起是不對的語法
--> 靠do j 定義J, 條件改為if , check 不要out of range [1 to row(x)].
proc iml;
use b;
read all var _all_ into x;
do i=1 to nrow(x);
if x[i,3] = 1
then
j=max(1,i-50);
do while(i-50<=j<=min(i+50,nrow(x)));
x[j,6] = 1;
j=j+1;
end;
end;
create new from x;
append from x;
quit;
不过用矩阵的指针操作更方便:
proc iml;
use b;
read all var _all_ into x;
do i=1 to nrow(x);
if x[i,3] = 1
then
x[max(i-50,1):min(i+50,nrow(x)),6] = 1;
end;
create new from x;
append from x;
quit;