2014-02-14 70 views
1

如何刪除SAS數據步驟中的重複項。如何刪除SAS數據中的重複步驟

data uscpi; 
     input year month cpi; 
    datalines; 
    1990 6 129.9 
    1990 7 130.4 
    1990 8 131.6 
    1990 9 132.7 
    1991 4 135.2 
    1991 5 135.6 
    1991 6 136.0 
    1991 7 136.2 
    ; 
    run; 

PROC SORT DATA = uscpi OUT = uscpi_dist NODUPKEY; 
BY year ; 
RUN; 

我可以使用proc步驟,但是如何在數據步驟中將其刪除。在此先感謝

+1

這裏是一種用散列對象做到這一點:http://stackoverflow.com/a/5705176/17743 – cmjohns

+0

哪些是你想保持?只要按照「年份」進行操作,就會隨機刪除記錄。我不認爲這是你想要的? – Victor

回答

6

在使用by-group處理時,您可以使用由SAS創建的自動變量的first.last.。他們更多地控制你認爲哪一行是重複的。 請閱讀使用說明書,以understand by group processing in a Data Step

data uscpi_dedupedByYear; 
set uscpi_sorted; 
by year; 
if first.year; /*only keep the first occurence of each distinct year. */ 
/*if last.year; */ /*only keep the last occurence of each distinct year*/ 

run; 

在很大程度上取決於誰是你的輸入數據集進行排序。例如:如果您的輸入數據集按年份排序&月份並且您使用if first.year;那麼您可以看到它只保留任何給定年份中的最早月份。但是,如果您的數據集按year & descending month排序,那麼if first.year;會保留上個月的任何給定年份。

這種行爲顯然不同於nodupkey的工作方式。

+0

謝謝你的回覆。 – santosh315345

-1

你可以設置一個標誌和德爾它具有0.1

data nodupsdataset; 
set work.sorteddataset; 
by store_nbr; 
if first.store_nbr 
then flag = 1; 
else 
flag=0; 
if flag=0 then delete; 

run;