sql - 如何根据条件删除某些组中的某些观察值
问题描述
我想根据条件删除某些组中的一些观察结果。我的条件是每个组:如果 (imput="toto") 然后删除但如果整个组有 imput="toto" 然后保留它
data temp;
input siren $ class $ imput $;
cards;
A CP titi
B CP toto
C CE tata
D CE tata
F CM toto
G CM toto
H SU tata
I SU toto
;
run;
我想要的输出:
siren class imput
A CP titi
C CE tata
D CE tata
F CM toto
G CM toto
H SU tata
提前谢谢了 !
解决方案
So you want to do two checks. Is the value toto and is the value only toto. Here is SQL that will do that. I had it explicitly create the checks as new variables so you can see what is happening. To see the check values for all observations remove the HAVING clause. If you are happy with it you can remove the check variables and just move the conditions into the having clause.
data temp;
row+1;
input siren $ class $ imput $;
cards;
A CP titi
B CP toto
C CE tata
D CE tata
F CM toto
G CM toto
H SU tata
I SU toto
;
proc sql ;
create table want as
select *
, imput ne 'toto' as check1
, max(imput ne 'toto') as check2
from temp
group by class
having check1 or not check2
order by row
;
quit;
To do this with just a DATA step you will want add a "double DOW Loop" so you can calculate the overall flag for a group in the first loop and then process the individual rows in the second loop. Note this requires the input dataset is sorted (or at least grouped) by class.
data want;
do until(last.class);
set temp;
by class notsorted;
if imput ne 'toto' then check2=1;
end;
do until(last.class);
set temp;
by class notsorted;
if imput ne 'toto' or not check2 then output;
end;
drop check2;
run;
推荐阅读
- scala - 如何在 AWS 上执行一个简单的 Jar 以对 S3 存储桶执行一些操作
- visual-studio-code - 使用大量 CPU 的 Visual Studio Code
- ios - IOS/Objective-C:从共享实例更改视图控制器中的属性?
- regex - 使用正则表达式,选择前导斜杠但仅当字符串为 2 或更多时?
- android - 带有 GridLayoutManager 的 RecyclerView 中的项目的高度不是 wrap_content
- python - 如何将字符串添加到 Python 3 中文本文件中读取的字符串?
- android - 使用 AsyncTask 类的简单计数器
- php - 如何使用 PHP 和 NuSOAP 将属性添加到 XML 响应中的标记?
- python - GeoServer REST API - 通过 PUT 请求创建图层时,如何指定与文件名称不同的图层名称?
- sql - 提取所有最低地址