这是一个快速而肮脏的解决方案,但它有效。我首先将其分解为三个单独的 sql 语句:
*count per patient/ndc;
proc sql;
create table step1 as
select pat_seqno, ndc_seqno, count(*) as ndc_count
from d1
group by pat_seqno, ndc_seqno
;
quit;
* maxcount per patient;
proc sql;
create table step2 as
select pat_seqno, max(ndc_count) as ndc_count
from step1
group by pat_seqno
;
quit;
*join count and maxcount;
proc sql;
create table want as
select t1.*
from step1 t1
inner join step2 t2
on t1.pat_seqno = t2.pat_seqno
and t1.ndc_count = t2.ndc_count
;
quit;
如果你愿意,你可以将它组合成一个 SQL 语句
proc sql;
create table want as
select t1.*
from
(
select pat_seqno, ndc_seqno, count(*) as ndc_count
from d1
group by pat_seqno, ndc_seqno
) t1
inner join (
select pat_seqno, max(ndc_count) as ndc_count
from (
select pat_seqno, ndc_seqno, count(*) as ndc_count
from d1
group by pat_seqno, ndc_seqno
)
group by pat_seqno
) t2
on t1.pat_seqno = t2.pat_seqno
and t1.ndc_count = t2.ndc_count
;
quit;