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

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

问题描述

考虑以下示例:

/* 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;

所以数据集ones包含一个变量A和3个观测值,所有的数据集编号包含一个变量(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 Program Editor 或 Enhanced Editor(不是 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:set 语句中隐藏的保留语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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