2013-11-15 244 views
0

我必須擺脫對象的,如果它滿足條件。SAS和PROC SQL

DATA:

Name Value1 
A  60 
A  30 
B  70 
B  30 
C  60 
C  50 
D  70 
D  40 

我想如果值= 30,然後兩個線路應在theoutput不來的。

期望outpu是

Name Value1 
C  60 
C  50 
D  70 
D  40 

我已經寫在PROC SQL代碼爲

proc sql; 
    create table ck1 as 
    select * from ip where name in 
    (select distinct name from ip where value = 30) 
    order by name, subject, folderseq; 
quit; 
+0

有啥問題? – Mureinik

回答

3

更改您的SQL是:

proc sql; 
    create table ck1 as 
    select * from ip where name not in 
    (select distinct name from ip where value = 30) 
    order by name, subject, folderseq; 
quit; 
0

數據步法:

data have; 
input Name $ Value1; 
datalines; 
A  60 
A  30 
B  70 
B  30 
C  60 
C  50 
D  70 
D  40 
;;;; 
run; 
data want; 
do _n_ = 1 by 1 until (last.name); 
    set have; 
    by name; 
    if value1=30 then value1_30=1; 
    if value1_30=1 then leave; 
end; 
do _n_ = 1 by 1 until (last.name); 
    set have; 
    by name; 
    if value1_30 ne 1 then output; 
end; 
run; 

以及交替,在某些情況下,避免了第二組語句時value1_30爲1稍快的方法(這是特別快,如果大部分都在他們30,所以你只保留少量的記錄)。

data want; 
do _n_ = 1 by 1 until (last.name); 
    set have; 
    by name; 
    counter+1; 
    if first.name then firstcounter=counter; 
    else if last.name then lastcounter=counter; 
    if value1=30 then value1_30=1; 
    if value1_30=1 then leave; 
end; 
if value1_30 ne 1 then 
    do _n_ = firstcounter to lastcounter ; 
    set have point=_n_; 
    output; 
    end; 
run; 
0

另一個SQL選項...

proc sql number; 
select 
a.name, 
a.value1, 
case 
when value1 = 30 then 1 
       else 0 
end as flag, 
sum(calculated flag) as countflagpername 
from have a 
group by a.name 
having countflagpername = 0 
;quit;