在SAS中识别背靠背日期
我有一个看起来像这样的数据集:
ID start_date end_date
1 01/01/2022 01/02/2022
1 01/02/2022 01/05/2022
1 01/06/2022 01/07/2022
2 01/09/2019 01/22/2022
2 06/07/2014 09/10/2015
3 11/10/2012 02/01/2013
我正在尝试创建一个虚拟指示器来显示背靠背的事件。到目前为止,我已经能够执行以下操作:
data df_1;
set df_2;
by ID end_date;
lag_epi_e = lag(end_date);
if not (first.ID) then do;
date_diff= start_date- lag(end_date);
end;
format lag_epi_e date9.;
run;
此代码的问题是,它将创建一个指标,以表明事件背靠背,但并未为第一个事件创建指标,而只有后续事件。这是一个以下外观的示例:
ID start_date end_date b2b_ind
1 01/01/2022 01/02/2022 0
1 01/02/2022 01/05/2022 1
1 01/06/2022 01/07/2022 1
如何重写代码,以便所有事件在背靠背时都符合1的指标?
I have a dataset that looks like this:
ID start_date end_date
1 01/01/2022 01/02/2022
1 01/02/2022 01/05/2022
1 01/06/2022 01/07/2022
2 01/09/2019 01/22/2022
2 06/07/2014 09/10/2015
3 11/10/2012 02/01/2013
I am trying to create a dummy indicator to show events that are back-to-back. So far, I have been able to do the following:
data df_1;
set df_2;
by ID end_date;
lag_epi_e = lag(end_date);
if not (first.ID) then do;
date_diff= start_date- lag(end_date);
end;
format lag_epi_e date9.;
run;
The issue with this code is that it will create an indicator to show that events are back to back but is does not create an indicator for the first event, only the follow up events. Here is an example of how it looks below:
ID start_date end_date b2b_ind
1 01/01/2022 01/02/2022 0
1 01/02/2022 01/05/2022 1
1 01/06/2022 01/07/2022 1
How can I rewrite the code so that all events take on an indicator of 1 when they are back-to-back?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您也想要第一张记录吗?
如果是这样,您可以设置该设置,但是如果下一个记录集不背靠背,会发生什么?
可能有助于显示您的预期输出。
注意,您还应在if语句之外使用计算出的滞后变量
否则,您将获得意外的结果。
Do you want 1 at first record as well?
If so you can set that, but what happens if the next record set is not back to back?
May help to show your expected output.
Note you should also use the calculated lag variable outside the IF statement
otherwise you'll get unexpected results.
在您的情况下,您需要检查领先的事件和滞后事件是否在一起。由于
LEAD
不是SAS中的功能,因此您可以使用众多方法之一来完成它。我最喜欢的是 sgf论文:SAS®中的铅(和滞后):一个问题,许多解决方案让我们为您的数据添加潜在客户。此代码正在做三件事:
df_1
在“背景”中start_date
并保存它 代码,则将其设置为丢失:
输出:
如果您有SAS/ETS,则可以在两次通过中执行此操作:
In your case, you'll want to check if both a leading and lagging event are butted up together. Since
lead
is not a function in SAS, you can use one of the many ways to accomplish it. My favorite is from this SGF paper: Calculating Leads (and Lags) in SAS®: One Problem, Many SolutionsLet's add a lead to your data. This code is doing three things:
df_1
in the "background"start_date
and saving it to a variableCode:
Output:
You can optionally do this in two passes if you have SAS/ETS: