Let's assume data set HAVE is sorted by PATIENT and ADMISSION_DATE. Each PATIENT/ADMISSON_DATE combination can have multiple records, some of which have variable TYPE="surgery". Then a double-do-until-last.by_var technique can be used:
data want;
do recnum=1 by 1 until (last.patient);
set have;
by patient admission_date;
where type='surgery';
if first.admission_date then wantrec=recnum;
end;
do recnum=1 by 1 until (last.patient);
set have;
by patient;
where type='surgery';
if recnum=wantrec then output;
end;
run;
Both DO UNTIL loops read and count only "surgery" records. The first DO UNTIL keeps updating WANTNUM so that it is set to the earliest surgery-record number ("if first.admission_date") within an admission date. Of course the last time WANTNUM is updated is for the most recent admission_date. The second DO UNTIL re-reads the same records and outputs only the one matching WANTNUM.
Note the "if first.admission_date" says to test if the record-in-hand is the start of a new admission_date. But we know it is actually the first "surgery" record within the date (not neccessarily the first overall record within date), because of the filtering of the where type='surgery' statement
... View more