2016-03-11 13 views
0

我正在嘗試瀏覽日期列表,並且僅保留5次或更多次出現的日期的日期範圍,並刪除所有其他日期範圍。我的例子是:SAS:首先使用。最後。處理日期範圍

data test; 
    input dt dt2; 
    format dt dt2 date9.; 
    datalines; 
    20000 20001 
    20000 20002 
    20000 20003 
    21000 21001 
    21000 21002 
    21000 21003 
    21000 21004 
    21000 21005 
    ; 
run; 

proc sort data = test; 
    by dt dt2; 
run; 

data check; 
    set test; 
    by dt dt2; 
    format dt dt2 date9.; 
    if last.dt = first.dt then 
     if abs(last.dt2 - first.dt) < 5 then delete; 
run; 

我想回到僅僅是一個入口,如果可能的話,但我會很樂意與整個適當的範圍內爲好。 的一個條目將是有一個表:

start_dt end_dt 
21000 21005 

合適的範圍是:

 21000 21001 
    21000 21002 
    21000 21003 
    21000 21004 
    21000 21005 

根據需要我的代碼不能正常工作,我不知道什麼樣的變化,我需要做。

回答

4

last.dt2first.dt是標誌,並且可以在(0,1)中有值,因此條件abs(last.dt2 - first.dt) < 5始終爲真。 使用計數器變量來計算記錄組,而不是:

data check(drop= count); 
    length count 8; 
    count=0; 
    do until(last.dt); 
     set test; 
     by dt dt2; 
     format dt dt2 date9.; 
     count = count+1; 
     if last.dt and count>=5 then output; 
    end; 
run; 
0

我不知道爲什麼你正在尋找使用last.dt2和您的刪除功能中的first.dt所以我把它周圍創建你想要的輸出:

data check2; 
set test; 
    by dt ; 
    format dt dt2 date9.; 
    if last.dt then do; 
     if abs(dt2 - dt) >= 5 then output; 
    end; 
run; 

當然,這隻會在你的文件在dt和dt2上排序時才起作用。

希望這會有所幫助。