k****i 发帖数: 347 | 1 data如下,怎么写code比较efficient
id item
1 A
1 C
1 K
2 B
2 C
2 U
2 Z
3 A
...
找出同时有B和C的id |
k*******a 发帖数: 772 | 2 try this, not sure about its efficiency
data test;
input id item $;
datalines;
1 A
1 C
1 K
2 B
2 C
2 U
2 Z
3 A
;
run;
proc sql;
select distinct id
from test
group by id
having sum(item="B") and sum(item="C");
quit; |
l********t 发帖数: 254 | 3 what does the Sum (item='B') do here?
Thanks.
【在 k*******a 的大作中提到】 : try this, not sure about its efficiency : data test; : input id item $; : datalines; : 1 A : 1 C : 1 K : 2 B : 2 C : 2 U
|
A*****a 发帖数: 1091 | 4 应该是summation的意思吧
【在 l********t 的大作中提到】 : what does the Sum (item='B') do here? : Thanks.
|
P****D 发帖数: 11146 | 5 哪天我要是统治宇宙了,先立个法,规定写条件的时候不许省略“>0”。
——一个强迫症患者的怨念
【在 k*******a 的大作中提到】 : try this, not sure about its efficiency : data test; : input id item $; : datalines; : 1 A : 1 C : 1 K : 2 B : 2 C : 2 U
|
k****i 发帖数: 347 | 6 确实应该加>0,不过就我给的temporary data来说不加是可以的
还有别的做法吗?
【在 P****D 的大作中提到】 : 哪天我要是统治宇宙了,先立个法,规定写条件的时候不许省略“>0”。 : ——一个强迫症患者的怨念
|
l****u 发帖数: 529 | 7 proc sql;
select distinct id
from test
group by id
having sum(max(item="B"),max(item="C"))=2;
quit; |
p******s 发帖数: 76 | |
s******r 发帖数: 1524 | 9 It should work.
proc sql;
select distinct id
from test
where item in ("B","C")
group by id
having count(distinct item)>1;
quit;
It should be faster.
proc sql;
select distinct id
from test
where item in ("B","C")
group by id
having max(item) > min(item) ;
quit;
for some database. way below may be the fastest. It depends.
proc sql;
select distinct id from (
select distinct id,item
from test
where item in ("B","C")
group by id,item
)
group by id
having count(*)>1;
quit;
【在 k****i 的大作中提到】 : data如下,怎么写code比较efficient : id item : 1 A : 1 C : 1 K : 2 B : 2 C : 2 U : 2 Z : 3 A
|