SAS:在集合语句中隐藏保留语句? [英] SAS: Hidden retain statement inside set statement?

查看:22
本文介绍了SAS:在集合语句中隐藏保留语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下示例:

/* Create two not too interesting datasets: */
Data ones (keep = A);
Do i = 1 to 3;
A = 1;
output;
End;
run;

Data numbers;
Do B = 1 to 5;
output;
End;
Run;

/* The interesting step: */
Data together;
Set ones numbers;
if B = 2 then A = 2;
run;

所以数据集包含一个变量 A 和 3 个观测值,全为 1,数据集编号包含一个变量 (B) 和 5 个观测值:数字 1 到 5.我希望生成的数据集一起有两列(A 和 B),并且 A 列(垂直)读取 1、1、1、.., 2, ., ., .

So dataset ones contains one variable A with 3 observations, all ones and dataset numbers contains one variable (B) with 5 observations: the numbers 1 to 5. I expect the resulting dataset together to have two columns (A and B) and the A column to read (vertically) 1, 1, 1, . , 2, . , . , .

但是,在执行代码时,我发现 A 列显示为 1, 1, 1, ., 2, 2, 2 , 2

However, when executing the code I find that column A reads 1, 1, 1, . , 2, 2, 2 , 2

显然,在第五次观察中创建的 2 一直保留下来,没有明显的原因.这是怎么回事?

Apparently the 2 created in the fifth observation is retained all the way down for no apparent reason. What is going on here?

(为了完整起见:当我将最后一个数据步骤分成两部分时:

(For the sake of completeness: when I split the last data step into two as below:

Data together;
set ones numbers;
run;
Data together;
set together;
if B = 2 then A = 2;
run;

它确实符合我的预期.)

it does do what I expect.)

推荐答案

是的,在 SETMERGEUPDATE<中定义的任何变量/code> 语句自动保留(在数据步骤循环的顶部未设置为缺失).你可以有效地忽略它

Yes, any variable that is defined in a SET, MERGE, or UPDATE statement is automatically retained (not set to missing at the top of the data step loop). You can effectively ignore that with

output;
call missing(of <list of variables to clear out>);
run;

在数据步骤结束时.

顺便说一句,这就是 MERGE 对多对一合并的工作方式,也是多对多合并通常不能按您希望的方式工作的原因.

This is how MERGE works for many-to-one merges, by the way, and the reason that many-to-many merges don't usually work the way you want them to.

一起"和分开"案例之间的区别在于,在分开的案例中,您有两个具有不同变量的数据集.如果您在交互模式下运行,即 SAS 程序编辑器或增强编辑器(不是 EG 或批处理模式),您可以使用数据步调试器更清楚地看到这一点.您会看到以下内容:

The difference between the 'together' and the 'separate' cases is that in the separate case, you have two data sets with different variables. If you are running this in interactive mode, ie SAS Program Editor or Enhanced Editor (not EG or batch mode), you can use the data step debugger to see this a little more clearly. You would see the following:

ones 数据集最后一行的末尾:

At the end of the last row of the ones dataset:

i A B
3 1 .

通知 B 存在,但丢失了.然后它回到数据步骤循环的顶部.所有三个变量都被单独留下,因为它们都来自数据集.然后它尝试再次读取 ones 一次,生成:

Notice B exists, but is missing. Then it goes back to the top of the data step loop. All three variables are left alone since they're all from the data sets. Then it attempts to read from ones one more time, which generates:

i A B
. . .

然后它意识到它无法从 ones 读取,并开始从 numbers 读取.numbers 数据集第一行的末尾:

Then it realizes it cannot read from ones, and starts to read from numbers. At the end of the first row of the numbers dataset:

i A B
. . 1

然后它到达顶部,再次没有任何变化;然后它为 B 读入 2.

Then it goes to the top, again changes nothing; then it reads in a 2 for B.

i A B
. . 2

然后根据您的程序将 A 设置为 2:

Then it sets A to 2, per your program:

i A B
. 2 2

然后它再次返回到数据步循环的开始.

Then it returns to the start of the data step loop again.

i A B
. 2 2

然后它读入 B=3:

i A B
. 2 3

然后它继续循环,对于 B=4, 5.

Then it continues looping, for B=4, 5.

现在,将其与单个数据集进行比较.它将几乎相同(在不会产生不同结果的数据集之间切换时存在细微差异).现在我们进入 A=2 B=2 的步骤:

Now, compare that to the single dataset. It will be nearly the same (with a small difference at the switch between datasets that does not yield a different result). Now we go to the step where A=2 B=2:

i A B
. 2 2

现在,当数据步骤读取下一行时,它上面包含所有三个变量.所以它产生:

Now when the data step reads in the next row, it has all three variables on it. So it yields:

i A B
. . 3

因为它读入 A=.从行中,它将其设置为丢失.在单数据步版本中,它没有可供 A 读取的值,因此它没有将 2 替换为缺失.

Since it read in A=. from the row, it is setting it to missing. In the one-data-step version, it didn't have a value for A to read in, so it didn't replace the 2 with missing.

这篇关于SAS:在集合语句中隐藏保留语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆