Starting from the Have dataset from the previous answers, this gives the same result: proc sql; create table want as select a.*, case when a.Facility_type='Nursing Home' and b.facility_type='Hospital' and b.admit_date le a.disch_date then 1 else 0 end as flag from have a left join have b on a.patient_id=b.patient_id and b.admit_date gt a.admit_date group by a.patient_id, a.admit_date having b.admit_date=min(b.admit_date); quit;
... View more