SAS 中的 IF-THEN 与 IF [英] IF-THEN vs IF in SAS

查看:152
本文介绍了SAS 中的 IF-THEN 与 IF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IFIF-THEN 有什么区别

例如下面的语句

if type='H' then output;

vs

if type='H';
output;

推荐答案

if-then 语句有条件地执行代码.如果满足给定观察的条件,则在执行 ; 之前then"后面的任何内容,否则不执行.在您的示例中,由于接下来是 output,因此只有 'H' 类型的观察结果会输出到数据步骤正在构建的数据集.您还可以使用 if-then-do 语句,例如在以下代码中:

An if-then statement conditionally executes code. If the condition is met for a given observation, whatever follows the 'then' before the ; is executed, otherwise it isn't. In your example, since what follows is output, only observations with type 'H' are output to the data set(s) being built by the data step. You can also have an if-then-do statement, such as in the following code:

if type = 'H' then do;
i=1;
output;
end;

If-then-do 语句有条件地执行 do;end; 之间的代码.因此,上述代码仅在 type 等于 'H' 时才执行 i=1;output;.

If-then-do statements conditionally execute code between the do; and the end;. Thus the above code executes i=1; and output; only if type equals 'H'.

没有 thenif 是子集 if".根据 SAS 文档:

An if without a then is a "subsetting if". According to SAS documentation:

子集 IF 语句测试观察后的条件读入程序数据向量 (PDV).如果条件为真,SAS继续处理当前的观察.否则,观察被丢弃,处理继续下一个观察.

A subsetting IF statement tests the condition after an observation is read into the Program Data Vector (PDV). If the condition is true, SAS continues processing the current observation. Otherwise, the observation is discarded, and processing continues with the next observation.

因此,如果不满足子集 if(例如 type='H')的条件,则观察不会输出到数据步骤创建的数据集.在您的示例中,只会输出类型为 'H' 的观察结果.

Thus if the condition of a subsetting if (ex. type='H') is not met, the observation is not output to the data set being created by the data step. In your example, only observations where type is 'H' will be output.

总而言之,您的两个示例代码产生相同的结果,但方式不同.if type='H' then output; 只输出类型为 'H' 的观察,而 if type='H';output; 丢弃类型不是 'H' 的观察.请注意,在后者中,您不需要 output; 因为在 SAS 数据步骤中有一个隐式输出,只有在有显式 output; 时才会被覆盖命令.

In summary, both of your example codes produce the same result, but by different means. if type='H' then output; only outputs observations where type is 'H', while if type='H'; output; discards observations where type is not 'H'. Note that in the latter you don't need the output; because there is an implicit output in the SAS data step, which is only overridden if there is an explicit output; command.

这篇关于SAS 中的 IF-THEN 与 IF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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