SAS和proc sql

时间:2013-11-15 12:34:22

标签: sql sas proc

如果满足条件,我必须摆脱一个主题。

DATA:

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

我想要的是如果值= 30则两条线都不应该在输出中。

期望的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;

3 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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时避免使用第二个set语句(如果大多数都有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;

答案 2 :(得分: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;