[英]SAS and proc sql
I have to get rid of a subject if it satisfies a condition. 如果满足条件,我就必须摆脱一个主题。
DATA: 数据:
Name Value1
A 60
A 30
B 70
B 30
C 60
C 50
D 70
D 40
What I want is if the value=30 then both the lines should not come in theoutput. 我想要的是,如果value = 30,那么这两行都不应该出现在输出中。
Desired outpu is 期望的outpu是
Name Value1
C 60
C 50
D 70
D 40
I have written a code in proc sql as 我已经在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;
Change your SQL to be: 将您的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 step method: 数据步长法:
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;
And an alternate, slightly faster method in some cases that avoids the second set statement when value1_30 is 1 (this is faster in particular if most have a 30 in them, so you're only keeping a small number of records). 还有一种替代的,稍快的方法,在某些情况下,当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;
Another SQL option... 另一个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.