arrays - 数组调用缺失与将数组的每个元素设置为零之间的区别

标签 arrays sas

考虑

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/

相关文章:

php - 在 PHP 中显示数组值

.net - NHibernate 中只读列表的最佳实践是什么

vba - SAS VBA 连接

python - Pandas read_sas错误: 'ascii' codec can't decode byte 0xd8 in position 0: ordinal not in range(128)

c++ - 指向常量数组的指针的正确定义是什么?

c# - 定义动态数组

javascript - 如何按顺序对数组中的重复值求和

testing - SAS 过程逻辑 : Test statement does not recognize categorical variables?

date - SAS 无效 'leap year' 日期问题 yymmdd8

date - 将字符日期变量转换为 SAS 日期