proc sql;
create table scores_surglt16 as
select
T1.*,
case when ILV2.Visit gt 0 then 'Y' end as Surgery_Flag
from Scores as T1
left join (
select *
from Surgery
where Visit lt 16
) as ILV2
on T1.visit=ILV2.visit
and T1.subject_id=ILV2.sid;
quit;
/* a consulat acima pode ser feita de modo menos verboso assim */
proc sql;
create table scores_surglt16_2 as
select
T1.*,
case when T2.Visit gt 0 then 'Y' end as Surgery_Flag
from Scores T1
natural left join Surgery(
where=(visit lt 16)
rename=(sid=subject_id)
) T2;
quit;