Hi everyone,
I have a problem doing mulptile talbes full join correctly. I'm hoping someone could help me point out what cause the error in my final joined table.
I have four datasets, each row has id (DESY_SORT_KEY), a dichomas variable on condition of interest, year of treatment/diagnosis, and a year variable with value of 2011/2012/2013. So in each of the longitudinal dataset, each id has three rows indicating yearly data (2011-2013). The four have datasets have overlapping ids but not exactly the same. In the final want dataset, i want to have all the ids from the four have datasets and the combined information by two link variables:DESY_SORT_KEY and longitudinalYear. My final dataset using the code below generates duplicate id and year combo because i'm joining the tables incorrectly. FYI, I checked back with my four original datasets. There are no duplicate id and year comobo. I'm hoping someone could help point out what went wrong with my code. Thank you for your help!
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 b.DESY_SORT_KEY=c.DESY_SORT_KEY and b.year=c.year
full join temp.longPapCompNodup as d
on c.DESY_SORT_KEY=d.DESY_SORT_KEY and c.year=d.year
;
quit;
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;
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;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.