[英]In SAS how to check whether the date in a data set exists in the range of dates in another data set
I have two data set Set1 and Set2. 我有两个数据集Set1和Set2。
Set1 data set has column Curr_Dt:- Set1数据集的列Curr_Dt:-
Set1
Curr_Dt
23/04/1998
01/01/2017
01/12/2018
10/10/2010
Set2 data set has 3 columns St_Dt, End_Dt, Ind Set2数据集具有3列St_Dt,End_Dt,Ind
St_Dt End_Dt Ind
01/11/2018 31/12/2018 N
01/01/1998 31/05/1998 N
30/11/2016 02/02/2017 N
I want to update the Ind column of Set2 data set to Y if Curr_Dt from Set1 is falling in between St_Dt and End_Dt of Set2. 如果来自Set1的Curr_Dt落在Set2的St_Dt和End_Dt之间,我想将Set2数据的Ind列更新为Y。
I don't see a key on here to merge by, so I am assuming that the first row goes with the first row and so down each data set. 我在此处看不到要合并的键,因此我假设第一行与第一行一致,因此在每个数据集下方。
You can do this with a simple Data Step. 您可以通过一个简单的数据步骤来执行此操作。
data want;
merge set1 set2;
if st_dt <= curr_dt <= end_dt then
ind = 'Y';
run;
This also assumes the dates are stored as dates and not strings. 这也假定日期存储为日期而不是字符串。
Create sets 创建集
data Set1;
length Curr_Dt $10;
input Curr_Dt;
cards;
23/04/1998
01/01/2017
01/12/2018
10/10/2010
;
run;
data Set2;
length St_Dt $10 End_Dt $10 Ind $1;
input St_Dt$ End_Dt$ Ind$;
cards;
01/11/2018 31/12/2018 N
01/01/1998 31/05/1998 N
30/11/2016 02/02/2017 N
30/11/2005 02/02/2005 N
run;
Set date formats 设定日期格式
data Set1;
set Set1;
Curr = input(Curr_Dt, ddmmyy10.);
run;
data Set2;
set Set2;
St = input(St_Dt, ddmmyy10.);
End = input(End_Dt, ddmmyy10.);
run;
Set Y flag if any Curr_Dt
from Set1
falls between St_Dt
and End_Dt
如果来自Set1
任何 Curr_Dt
介于St_Dt
和End_Dt
之间,则设置Y标志
proc sql;
create table Set2 as
select distinct St_Dt, End_Dt,
case when Set1.Curr>Set2.St and Set1.Curr<Set2.End
then 'Y' else 'N' end as Ind
from Set2
left join Set1 on Set1.Curr>Set2.St and Set1.Curr<Set2.End;
run;
You get 你得到
01/01/1998 31/05/1998 Y
30/11/2016 02/02/2017 Y
01/11/2018 31/12/2018 Y
30/11/2005 02/02/2005 N
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.