<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Full join between 3 tables - SAS Data Integration Studio in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Full-join-between-3-tables-SAS-Data-Integration-Studio/m-p/825053#M20487</link>
    <description>&lt;P&gt;As others already shared first you need to have certainty how the SQL logic needs to look like ...and then you need to define this logic in DI Studio.&lt;/P&gt;
&lt;P&gt;Based on the sample data&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/234586"&gt;@A_SAS_Man&lt;/a&gt;&amp;nbsp;created for you below two SQL coding options&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
 input Id  A $1. ;
 datalines;
1 X
2 X
3 X
4 X
5 X
6 X
8 X
;

data b;
 input Id B $1.;
 datalines;
4 Y
5 Y
6 Y
7 Y
8 Y
9 Y
;

 data c;
 input Id C $1.;
 datalines;
1 Z
2 Z
8 Z
9 Z
10 Z
11 Z
;

/* option 1 */
proc sql;
  create table want as
    select 
    coalesce(a.id,b.id,c.id) as id,
    a.a,
    b.b,
    c.c
    from 
      a 
      full outer join b
        on a.id=b.id
      full outer join c
        on a.id=c.id or b.id=c.id
    order by id
  ;
quit;

/* option 2 */
proc sql;
  create view v_inter as
    select 
    coalesce(a.id,b.id) as id,
    a.a,
    b.b
    from 
      a full outer join b
        on a.id=b.id
  ;
quit;

proc sql;
  create table want as
    select 
    coalesce(i.id,c.id) as id,
    i.a,
    i.b,
    c.c
    from 
      v_inter i full outer join c
        on i.id=c.id
    order by id
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12887"&gt;@ErikLund_Jensen&lt;/a&gt;&amp;nbsp;hints it's sometimes not that intuitive to define a SQL using the SQL transformation even if you exactly know what syntax you want it to generate.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For this reason I sometimes used to "break" things up into more simple SQLs. In above code you could implement option 2 using two SQL transformation nodes - the first one creates the view, the second one then joins your 3rd table to the view to create the table you're after. Because the first SQL only creates a view going for two nodes in the DI flow won't have a negative impact on performance (a view is basically encapsulated SQL code that only gets executed when you use the view ...which is only in the 2nd SQL node).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 23 Jul 2022 11:11:36 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2022-07-23T11:11:36Z</dc:date>
    <item>
      <title>Full join between 3 tables - SAS Data Integration Studio</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Full-join-between-3-tables-SAS-Data-Integration-Studio/m-p/824934#M20483</link>
      <description>&lt;P&gt;Hello! How can I make this join between these 3 tables?&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="InsMaximiano_1-1658504538431.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/73612i325A4874B72A8FD6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="InsMaximiano_1-1658504538431.png" alt="InsMaximiano_1-1658504538431.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jul 2022 15:46:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Full-join-between-3-tables-SAS-Data-Integration-Studio/m-p/824934#M20483</guid>
      <dc:creator>InêsMaximiano</dc:creator>
      <dc:date>2022-07-22T15:46:15Z</dc:date>
    </item>
    <item>
      <title>Re: Full join between 3 tables - SAS Data Integration Studio</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Full-join-between-3-tables-SAS-Data-Integration-Studio/m-p/824978#M20484</link>
      <description>&lt;PRE&gt;&lt;CODE class=""&gt;data a;
 input Id  A $1. ;
 datalines;
1 X
2 X
3 X
4 X
5 X
6 X
 ;

data b;
 input Id B $1.;
 datalines;
4 Y
5 Y
6 Y
7 Y
8 Y
9 Y
 ;

 data c;
 input Id C $1.;
 datalines;
1 Z
2 Z
8 Z
9 Z
10 Z
11 Z
 ;

proc sql;
create table Output_Table as
	select case when a.Id is not missing
				then a.Id
				when b.Id is not missing
				then b.Id
				when c.Id is not missing
				then c.Id
			end as Id,
			a.A,
		   b.B,
		   c.C
	from a full outer join b 
		 on
		 (a.Id=b.Id)
		 full outer join c on
		 (a.Id=c.Id)
		 or
		 (b.Id=c.Id)
	order by Id
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Jul 2022 17:19:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Full-join-between-3-tables-SAS-Data-Integration-Studio/m-p/824978#M20484</guid>
      <dc:creator>A_SAS_Man</dc:creator>
      <dc:date>2022-07-22T17:19:19Z</dc:date>
    </item>
    <item>
      <title>Re: Full join between 3 tables - SAS Data Integration Studio</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Full-join-between-3-tables-SAS-Data-Integration-Studio/m-p/824998#M20485</link>
      <description>&lt;P&gt;If none of the data sets have repeats of the Id variable and are sorted by Id:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
   merge a b c;
   by id;
run;&lt;/PRE&gt;
&lt;P&gt;given the example data. If there are other variables involved then more details.&lt;/P&gt;
&lt;P&gt;If you wanted a point and click approach, can't help.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jul 2022 18:39:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Full-join-between-3-tables-SAS-Data-Integration-Studio/m-p/824998#M20485</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-07-22T18:39:11Z</dc:date>
    </item>
    <item>
      <title>Re: Full join between 3 tables - SAS Data Integration Studio</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Full-join-between-3-tables-SAS-Data-Integration-Studio/m-p/824999#M20486</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/417025"&gt;@InêsMaximiano&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As shown in the previous answer, the code for a join of 3 tables is not complicated in itself. But building the code in Data Integration Studio's Join transformation is a different matter. It is more complicated, and it is harder to debug and maintain afterwards. So my advice is: Dont do it. Use two joins instead and make a full join of two tables, and then join the result with the third table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Add the&amp;nbsp;expression coalesce(tab1.ID, tab2.ID) in the mapping pane in order to get the ID in the output table's ID column whether the input for the current row comes from one or the other or both input tables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you insist on using one join-transformation only, there is an explanation in the DI Studio Help, in the page "Adding a Join to an SQL Query on the Designer Tab". But it is so much easier to use two joins instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="nway_join.gif" style="width: 453px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/73642i84E005D1FDD6CF42/image-size/large?v=v2&amp;amp;px=999" role="button" title="nway_join.gif" alt="nway_join.gif" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jul 2022 18:42:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Full-join-between-3-tables-SAS-Data-Integration-Studio/m-p/824999#M20486</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2022-07-22T18:42:13Z</dc:date>
    </item>
    <item>
      <title>Re: Full join between 3 tables - SAS Data Integration Studio</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Full-join-between-3-tables-SAS-Data-Integration-Studio/m-p/825053#M20487</link>
      <description>&lt;P&gt;As others already shared first you need to have certainty how the SQL logic needs to look like ...and then you need to define this logic in DI Studio.&lt;/P&gt;
&lt;P&gt;Based on the sample data&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/234586"&gt;@A_SAS_Man&lt;/a&gt;&amp;nbsp;created for you below two SQL coding options&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
 input Id  A $1. ;
 datalines;
1 X
2 X
3 X
4 X
5 X
6 X
8 X
;

data b;
 input Id B $1.;
 datalines;
4 Y
5 Y
6 Y
7 Y
8 Y
9 Y
;

 data c;
 input Id C $1.;
 datalines;
1 Z
2 Z
8 Z
9 Z
10 Z
11 Z
;

/* option 1 */
proc sql;
  create table want as
    select 
    coalesce(a.id,b.id,c.id) as id,
    a.a,
    b.b,
    c.c
    from 
      a 
      full outer join b
        on a.id=b.id
      full outer join c
        on a.id=c.id or b.id=c.id
    order by id
  ;
quit;

/* option 2 */
proc sql;
  create view v_inter as
    select 
    coalesce(a.id,b.id) as id,
    a.a,
    b.b
    from 
      a full outer join b
        on a.id=b.id
  ;
quit;

proc sql;
  create table want as
    select 
    coalesce(i.id,c.id) as id,
    i.a,
    i.b,
    c.c
    from 
      v_inter i full outer join c
        on i.id=c.id
    order by id
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12887"&gt;@ErikLund_Jensen&lt;/a&gt;&amp;nbsp;hints it's sometimes not that intuitive to define a SQL using the SQL transformation even if you exactly know what syntax you want it to generate.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For this reason I sometimes used to "break" things up into more simple SQLs. In above code you could implement option 2 using two SQL transformation nodes - the first one creates the view, the second one then joins your 3rd table to the view to create the table you're after. Because the first SQL only creates a view going for two nodes in the DI flow won't have a negative impact on performance (a view is basically encapsulated SQL code that only gets executed when you use the view ...which is only in the 2nd SQL node).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 23 Jul 2022 11:11:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Full-join-between-3-tables-SAS-Data-Integration-Studio/m-p/825053#M20487</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-07-23T11:11:36Z</dc:date>
    </item>
  </channel>
</rss>

