简体   繁体   English

在SAS中如何检查数据集中的日期是否在另一个数据集中的日期范围内

[英]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_DtEnd_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.

相关问题 如何过滤特定日期范围内的数据集? - How to filter a data set on a specific date range? 如何在 SAS 数据集中的列中查找最小和最大日期? - How to find min and max date across columns in SAS data set? PHP 检查两个日期之间是否存在日期,然后设置一个变量 - PHP check if a date exists between 2 dates and then set a variable 如何在 python 中的数据集中排除一系列日期? - how can i exclude a range of dates in my data set in python? 当日期介于其他两个日期之间时,如何将数据集连接到另一个 - How can I connect a data set to another when the date is between 2 other dates R SQL查询检查两个日期之间是否存在日期 - SQL query to check whether a date exists between two dates 从数据集中查找重叠的日期范围 - Find overlapping date range from a data set 按日期对每个主题的子集SAS数据集进行分组 - Subsetting SAS data set by earliest date for each subject 检查日期是否在Selenium Web驱动程序(Java)中的日期范围内 - Check whether dates are within the date range in selenium web driver ( Java) 根据测量日期与另一个数据集中的其他两个日期比较,在一个数据集中标记行 - labelling rows in one data set based on the date of the measurement compared to two other dates in another dataset
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM