2013-11-15 201 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;