考虑
data want;
set have;
array ay1{7};
retain ay1:;
if first.tcol then do;
call missing(of ay1{*});
end;
if ay1{7} > a then do;
[other statements to determine value of ay1]
end;
else do;
[other statements to determine value of ay1]
end;
by tcol;
run;
由于数组ay1
会在观察之间自动保留,如果我想让程序做一些分组处理,我需要在遇到a时重新设置ay1
值新的 tcol
值(否则它将继承上次观察到的先前 tcol
值的值。这将影响 if ay{7} > a
,因此之后的所有其他陈述都将受到影响)。
在我目前的代码中,我将重置数组
do i = 1 to dim(ay1);
ay1{i} = 0;
end;
这项工作罚款。对于每个tcol
值的第一个obs,它会先将ay1
的值全部重置为0,然后执行[other statement]
更新ay1
在此观察中。
如果我使用 call missing(of ay1{*});
,对于每个 tcol
值的第一个 obs,它将设置 ay1 的每个值
丢失(如预期)。但是下面的[other statement]
将NOT更新ay1
。 (我在 [other statement]
中放置了几个 put 语句作为调试步骤,以确保这部分已运行。它完成除更新 ay1
值之外的所有其他工作)。
如果 first.tcol
失败,一切似乎都会恢复正常(没有错误,但输出数据集是错误的,因为每个 by group 的第一步都有所有意外值)。所以我觉得这里使用call missing
肯定有问题。
最佳答案
您的陈述“因为数组 ay1 将在观察之间自动保留”是不正确的。声明为 _TEMPORARY_
的数组会自动保留。永久变量数组不是。
您可以使用以下方法进行测试:
data want;
set have;
array ay1{7};
by tcol;
if first.tcol then do;
do i=1 to 7;
ay1[i] = 1;
end;
end;
run;
您会看到,在每个组中的第一个 tcol 值之后,值将丢失。 IE。它们没有保留在行之间。
添加
retain ay:;
到您的数据步骤,它应该按您预期的方式工作。
编辑:添加这个以表明它按描述工作。
data have;
tcol = 1;
a = 1;
output;
tcol = 1;
a = 10;
output;
run;
data want;
set have;
array ay1{7};
retain ay1:;
by tcol;
if first.tcol then do;
call missing(of ay1{*});
end;
if ay1{7} > a then do;
do i=1 to 7;
ay1[i] = -a;
end;
end;
else do;
do i=1 to 7;
ay1[i] = 999;
end;
end;
run;
关于arrays - 数组调用缺失与将数组的每个元素设置为零之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31473983/