2016-12-13 183 views
0

我有一张将被加载到oracle数据库中的表。 我需要删除重复值而不改变数据的顺序。 每组有5个可能的记录。 1.空行需要删除。 2.重复名称需要被删除,所以只出现不同的名称。 3.数据不能重新排序。在SAS中删除重复记录

1 Commingled Data 
2 Social Security 
3 
4 
5 SSA 1996 
1 Commingled Data 
2 Social Security 
3 
4 
5 SSA 1997 
1 Commingled Data 
2 Social Security 
3 
4 
5 SSA -1998 
1 Commingled Data 
2 Statistical Administrative 
3 
4 
5 StARS 2000 
1 Federal 
2 Treasury 
3 Internal 
4 1099 
5 Master File - TY 1997 (1099/IRMF) 
1 Federal 
2 Treasury 
3 Internal 
4 1099 
5 Master File - TY 1998 (1099/IRMF) 
1 State 
2 Wage 
3 Indiana 
4 
5 Indiana - 1990Q1-2005Q2 
1 Federal 
2 Treasury 
3 Internal 
4 1040 
5 TY 2003 (1040/IMF) 1% File 
1 Federal 
2 Treasury 
3 Internal 
4 1040 
5 TY 2003 (1040/IMF) Cycles 1-39 
+0

什么是“重复行”?你的输出是什么?你在想什么? – Joe

+0

可重复的行通常是第1行,第1行是混合数据,联邦也重复第2行也重复,有时第3行也是..我试图first.last函数,也coalescec和selfjoin并与偏移量。 – user601828

+0

你可以:第一步=>做一个选择不同的只选择不同的变量,然后第二步=>删除一个行,如果所有的变量都失踪? –

回答

1

这是HASH对象的一个​​很好的用例。如果您使用multidata:'n'ref方法,它将检查记录是否已经存在于散列表中,如果不存在,则添加它 - 但不添加重复项。

这里我添加rownum以便能够返回到原来的排序顺序,因为散列表是二叉树,并且没有自然顺序,除非您施加它。

data have; 
input @1 line $50.; 
datalines; 
1 Commingled Data 
2 Social Security 
3 
4 
5 SSA 1996 
1 Commingled Data 
2 Social Security 
3 
4 
5 SSA 1997 
1 Commingled Data 
2 Social Security 
3 
4 
5 SSA -1998 
1 Commingled Data 
2 Statistical Administrative 
3 
4 
5 StARS 2000 
1 Federal 
2 Treasury 
3 Internal 
4 1099 
5 Master File - TY 1997 (1099/IRMF) 
1 Federal 
2 Treasury 
3 Internal 
4 1099 
5 Master File - TY 1998 (1099/IRMF) 
1 State 
2 Wage 
3 Indiana 
4 
5 Indiana - 1990Q1-2005Q2 
1 Federal 
2 Treasury 
3 Internal 
4 1040 
5 TY 2003 (1040/IMF) 1% File 
1 Federal 
2 Treasury 
3 Internal 
4 1040 
5 TY 2003 (1040/IMF) Cycles 1-39 
;;;; 
run; 

data _null_; 
    set have end=eof; 
    rownum = _n_; 
    if _n_=1 then do; 
    declare hash h(ordered:'n', multidata:'n'); 
    h.defineKey('line'); 
    h.defineData('line', 'rownum'); 
    h.defineDone(); 
    end; 
    if not missing(substr(line,3)) then rc = h.ref(); 
    if eof then do; 
    h.output(dataset:'want'); 
    end; 
run; 

proc sort data=want; 
    by rownum; 
run; 
+0

乔,谢谢!没有人在工作可以解决这个问题。你太棒了。再次感谢 – user601828