2014-02-14 143 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;