BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Crystal_F
Quartz | Level 8

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; 
1 ACCEPTED SOLUTION

Accepted Solutions
collinelliot
Barite | Level 11

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

2 REPLIES 2
collinelliot
Barite | Level 11

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; 
Crystal_F
Quartz | Level 8
That's super super helpful!!! Thanks a lot. I think now it works!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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