<?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: SQL full join for multiple tables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SQL-full-join-for-multiple-tables/m-p/784156#M250188</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See if this is what you meant:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
/* SQL join - not doing the right thing */
proc sql;
  create table not_there_yet_want as
  select
     coalesce(a.name,b.name,c.name) as name
    ,coalesce(a.age,b.age,c.age) as age
    ,coalesce(a.weight,b.weight,c.weight) as weight
    ,coalesce(a.sex,b.sex,c.sex) as sex

  from class_A a

  full join class_B b
  on a.name=b.name

  full join class_C c
  on coalesce(a.name,b.name)=c.name
  order by name
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 05 Dec 2021 09:53:02 GMT</pubDate>
    <dc:creator>EyalGonen</dc:creator>
    <dc:date>2021-12-05T09:53:02Z</dc:date>
    <item>
      <title>SQL full join for multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-full-join-for-multiple-tables/m-p/784147#M250182</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;I can't figure out how in below code the SQL would need to look like so it returns the desired result as done via a data step merge.&lt;/P&gt;
&lt;P&gt;I'm looking for something "generic" that would also work for 6 source tables.&lt;/P&gt;
&lt;P&gt;I want in the end to generate the code using dictionary tables and I understand that I might end-up with a data step merge with all the renaming "galore" for performance reasons. ...but still really curious how a SQL version would need to look like and I just can't manage to come-up with something simple/a single SQL.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* create sample data */
data class_A;
  set sashelp.class(firstobs=1 obs=4);
  call missing(age,weight);
  keep name age weight sex;
run;
data class_B;
  set sashelp.class(firstobs=3 obs=6);
  call missing(age);
  keep name age weight sex;
run;
data class_C;
  set sashelp.class(firstobs=4 obs=8);
  keep name age weight sex;
run;

/* SQL join - not doing the right thing */
proc sql;
  create table not_there_yet_want as
  select
     coalesce(a.name,b.name,c.name) as name
    ,coalesce(a.age,b.age,c.age) as age
    ,coalesce(a.weight,b.weight,c.weight) as weight
    ,coalesce(a.sex,b.sex,c.sex) as sex

  from class_A a

  full join class_B b
  on a.name=b.name

  full join class_C c
  on a.name=c.name
  order by name
  ;
quit;

title 'SQL not returning desired result';
proc print data=not_there_yet_want;
run;
title;

/* desired result - but looking for a SQL version */
data desired;
  if 0 then set sashelp.class(keep=name age weight);
  merge
    class_a(rename=(age=_a_age weight=_a_weight))
    class_b(rename=(age=_b_age weight=_b_weight))
    class_c(rename=(age=_c_age weight=_c_weight))
    ;
  by name;
  age=coalesce(_a_age,_b_age,_c_age);
  weight=coalesce(_a_weight,_b_weight,_c_weight);
  drop _:;
run;

title 'Desired result - but not done using SQL';
proc print data=desired;
run;
title;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1638693188870.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66416i7DB85C768E2052B5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1638693188870.png" alt="Patrick_0-1638693188870.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Patrick&lt;/P&gt;</description>
      <pubDate>Sun, 05 Dec 2021 09:17:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-full-join-for-multiple-tables/m-p/784147#M250182</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-12-05T09:17:28Z</dc:date>
    </item>
    <item>
      <title>Re: SQL full join for multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-full-join-for-multiple-tables/m-p/784156#M250188</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See if this is what you meant:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
/* SQL join - not doing the right thing */
proc sql;
  create table not_there_yet_want as
  select
     coalesce(a.name,b.name,c.name) as name
    ,coalesce(a.age,b.age,c.age) as age
    ,coalesce(a.weight,b.weight,c.weight) as weight
    ,coalesce(a.sex,b.sex,c.sex) as sex

  from class_A a

  full join class_B b
  on a.name=b.name

  full join class_C c
  on coalesce(a.name,b.name)=c.name
  order by name
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Dec 2021 09:53:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-full-join-for-multiple-tables/m-p/784156#M250188</guid>
      <dc:creator>EyalGonen</dc:creator>
      <dc:date>2021-12-05T09:53:02Z</dc:date>
    </item>
    <item>
      <title>Re: SQL full join for multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-full-join-for-multiple-tables/m-p/784210#M250220</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13766"&gt;@EyalGonen&lt;/a&gt;&amp;nbsp;That did the trick. Thanks!&lt;/P&gt;</description>
      <pubDate>Sun, 05 Dec 2021 21:12:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-full-join-for-multiple-tables/m-p/784210#M250220</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-12-05T21:12:41Z</dc:date>
    </item>
  </channel>
</rss>

