如何解决删除日期两天内发生的行
又是我。 解释这个有点棘手,但我会尽力而为。 我有一个数据集,其中包含客户 ID、这些客户被呼叫的天数以及一个虚拟列,其中 1 是我们与某人交谈时,0 是我们未与某人交谈时。
data Have;
input ID callDate :ddmmyy. Contacted ;
format callDate ddmmyy10.;
datalines4;
001 30/11/2020 0
001 01/12/2020 1
001 30/12/2020 1
001 31/12/2020 1
001 01/01/2021 1
002 01/02/2021 1
002 02/02/2021 1
002 15/02/2021 1
;;;
run;
data Want;
input ID callDate :ddmmyy. Contacted ;
format callDate ddmmyy10.;
datalines4;
001 01/12/2020 1
001 01/01/2021 1
002 02/02/2021 1
002 15/02/2021 1
;;;
run;
我基本上只想保留成功联系并且在第二天或后天没有成功联系的记录。
例如,对于这样的连续几天:
day 1 - 0
day 2 - 1
day 3 - 0
day 4 - 1
day 5 - 0
day 6 - 1
我最终只会保留第 6 天。
我什至不知道从哪里开始真正...在 python 中,我会创建遍历行的嵌套循环,但在 proc sql
中我不知道如何处理。
如果有任何想法至少给我一个推动,我将不胜感激。
解决方法
您可以使用针对自身的相关子查询从表中进行选择。
SQL 将执行您在 Python 中编写的所有循环。
示例:
proc sql;
create table want as
select * from have as each
where
not exists (
select * from have as self
where self.calldate between each.calldate + 1 and each.calldate + 2 /* follow up */
and self.contacted = 1 /* with an assertion of contact */
)
;
,
有几种方法可以解决这个问题,其中之一是数据步骤。 SAS 在数据步骤中做得不好的一件事是向前看;然而,这里有一些很棒的技巧。这是您可以使用的一次性解决方案,它仅使用数据步骤,并且基于 Andrew Gannon 的 SGF 2019 论文 Calculating Leads and Lags in SAS: One Problem,Many Solutions。该解决方案可以用数组压缩,但经过扩展以使其更容易理解整体逻辑。
基本思想:使用一些 SAS 函数打开您正在阅读的数据集,并使用直接访问在其中获取未来的观察结果。我们可以通过简单地使用 _N_+lead
为每个观察提取未来值。由于我们已经在 set
语句中打开了当前正在读取的数据集,因此我们的 obs 提取对于每一行将始终保持同步。
data want;
set have;
by id callDate;
retain _dsid_;
/* Open up your current dataset */
if(_N_ = 1) then _dsid_ = open('have');
/* Fetch 1 obs ahead of time */
_lead1_rc_ = fetchobs(_dsid_,_N_+1);
/* Fetch values from 1 obs ahead using the dataset we opened */
lead1_date = getvarn(_dsid_,varnum(_dsid_,"callDate"));
lead1_contacted = getvarn(_dsid_,"Contacted"));
lead1_id = getvarn(_dsid_,"id"));
/* Fetch2 obs ahead */
_lead2_rc_ = fetchobs(_dsid_,_N_+2);
lead2_date = getvarn(_dsid_,"callDate"));
lead2_contacted = getvarn(_dsid_,"Contacted"));
lead2_id = getvarn(_dsid_,"id"));
/* Set future values to missing if we've crossed to the next ID */
if(lead1_id NE id) then call missing(lead1_date,lead1_contacted);
if(lead2_id NE id) then call missing(lead2_date,lead2_contacted);
/* Keep only records that a successful contact AND are not followed on the next day or the day after next by a successful contact */
if(contacted AND NOT ( (lead1_contacted AND (lead1_date - callDate LE 2) OR (lead2_contacted AND (lead2_date - callDate LE 2) ) ) ) );
format lead1_date lead2_date date9.;
run;
另一种解决方案可以使用 lead
中的 PROC EXPAND
函数,并将 where
逻辑添加为输出数据集选项:
proc expand data=have
out=want(drop=time
where=(contacted AND NOT ( (lead1_contacted AND (lead1_date - callDate LE 2) OR (lead2_contacted AND (lead2_date - callDate LE 2) ) ) ) )
);
by id;
convert callDate = lead1_Date / transform=(lead 1);
convert callDate = lead2_Date / transform=(lead 2);
convert contacted = lead1_contacted / transform=(lead 1);
convert contacted = lead2_contacted / transform=(lead 2);
run;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。