I think the below is what you're trying to do. See the "coalesce(..." added to the join conditions.
proc sql;
create table temp.balancedlongitudinal4POP as
select coalesce (a.DESY_SORT_KEY,b.DESY_SORT_KEY, c.DESY_SORT_KEY, d.DESY_SORT_KEY) as DESY_SORT_KEY,
coalesce (a.year,b.year, c.year, d.year)as LongitudinalYear,
coalesce (a.PSG, 0)as PSG, coalesce(a.year_PSGtesting,.)as year_PSGtesting,
coalesce (b.OSA_diag,0)as OSA_diag, coalesce(b.year_OSAdiagtesting,.) as year_OSAdiagtesting,
coalesce (c.PAPInit, 0) as PAP_Init, coalesce(c.year_PAPInit,.) as year_PAPInit,
coalesce (d.PAP_Comp, 0)as PAP_Comp
from temp.longPsgNodup as a
full join temp.longOSADiagNodup as b
on a.DESY_SORT_KEY=b.DESY_SORT_KEY and a.year=b.year
full join temp.longPapinitNodup as c
on coalesce(a.DESY_SORT_KEY, b.DESY_SORT_KEY) =c.DESY_SORT_KEY and
coalesce(a.year, b.year)=c.year
full join temp.longPapCompNodup as d
on coalesce(a.DESY_SORT_KEY, b.DESY_SORT_KEY, c.DESY_SORT_KEY) = d.DESY_SORT_KEY and
coalesce(a.year, b.year, c.year) = d.year;
quit;
... View more