SAS - 使用保留的数据步骤?

时间:2014-05-14 13:11:35

标签: sas retain datastep

我有以下数据......

acct    seq    start            end
1111     A     01/01/2014       01/31/2014
1111     A     02/01/2014       02/28/2014

我需要排除帐户的开始日期是结束日期后1天的记录。因此,上述记录将被排除,因为它的结束日期为2014年1月31日第一次观察,开始日期为2014年1月2日,以便进行以下观察。

我想我需要使用Retain来完成这个,但我不太清楚如何编写它。任何帮助将不胜感激......

2 个答案:

答案 0 :(得分:1)

Proc SQL解决方案......假设您正在使用SAS日期...

proc sql;
    create table excludes as
    select distinct acct
    from data as one
    left join data as two
    on one.acct=two.acct and one.end=two.start-1
    where two.start is not null;

    create table filtered as
    select *
    from data
    where acct not in
    (
        select *
        from excludes
    );
quit;

答案 1 :(得分:0)

Datastep解决方案,假设您的数据按acctseq分组(并且您的日期为SAS日期)

data want ;
  set have ;
  by acct seq ;

  retain prevdt . ;

  prevdt = end ;

  if first.seq then output ;
  else do ;
    if start > sum(prevdt,1) then output ;
    prevdt = end ;
  end ;

  drop prevdt ;
run ;
相关问题