滞后功能在 SAS 中不起作用 [英] lag function doesn't work in SAS

查看:48
本文介绍了滞后功能在 SAS 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的程序的一部分.(oldindex 和 oldreadmit 在保留推荐中)

Here is the part of my program.(oldindex and oldreadmit is in retain commend)

问题是它适用于 oldindex=1 然后 readmit=1 但不起作用 lag(oldreadmit)=1 然后 readmit=1.你能说出是什么问题吗?提前致谢!

The problem is it works for oldindex=1 then readmit=1 but doesn't work lag(oldreadmit)=1 then readmit=1. Could you tell what's the problem? Thanks in advance!

else if 0< gap <= 30 then do;
     index_d=0;
     if lag(oldreadmit)=1 or oldindex=1  then readmit=1;
         else oth=1;
     oldindex=index_d; 
     oldreadmit=readmit;
     end;

推荐答案

SAS lag 函数会引起很多混乱,但是一旦您了解它的工作原理,它就会变得有意义.假设您有 3 个观察值,并且您有一个 if 语句导致在处理过程中跳过第二个观察值.如果您随后将滞后函数应用于第三次观察,它将返回第一次观察,而不是第二次,因为最后一次处理任何观察是针对第一次观察.

The SAS lag function is a cause for much confusion, but once you understand how it works it makes sense. Suppose you have 3 observations, and you have an if statement that causes the second observation to be skipped during processing. If you then apply the lag function to the third observation, it will return the first observation, not the second, because the last time any observation was processed was for the first observation.

这意味着,在组合滞后和 if 语句时要小心.在您的代码中,只有在 if 语句为真时才会执行的子句中存在滞后.这会给你带来奇怪的结果.您应该做的是定义一个变量,例如 l_oldreadmit,以等于 before 在 if 语句中使用它的滞后.

What this means is, be careful when combining lags and if statements. In your code, you have a lag in a clause that will only be executed if an if statement is true. This will give you weird results. What you should do is define a variable, say l_oldreadmit, to equal the lag before using it in the if statement.

这会起作用:

l_oldreadmit = lag(oldreadmit);

if (... whatever you have here ...);
else if 0< gap <= 30 then do;
 index_d=0;
 if l_oldreadmit=1 or oldindex=1  then readmit=1;
     else oth=1;
 oldindex=index_d; 
 oldreadmit=readmit;
 end;

这篇关于滞后功能在 SAS 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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