problem with proc sql full join

Accepted Solution Solved
Reply
Contributor
Posts: 49
Accepted Solution

problem with proc sql full join

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 variablesSmiley Very HappyESY_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; 

Accepted Solutions
Solution
‎06-15-2017 10:50 AM
Super Contributor
Posts: 284

Re: problem with proc sql full join

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 solution in original post


All Replies
Solution
‎06-15-2017 10:50 AM
Super Contributor
Posts: 284

Re: problem with proc sql full join

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; 
Contributor
Posts: 49

Re: problem with proc sql full join

That's super super helpful!!! Thanks a lot. I think now it works!
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 95 views
  • 1 like
  • 2 in conversation