これは簡単で汚い解決策ですが、うまくいきます。最初に、3 つの個別の 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;