繁体   English   中英

SAS和proc SQL

[英]SAS and proc sql

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

数据:

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

我想要的是,如果value = 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;

将您的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;

数据步长法:

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;

另一个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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM