<?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 FULL JOIN with Multiple Datasets in ID variable (one record per ID) in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/FULL-JOIN-with-Multiple-Datasets-in-ID-variable-one-record-per/m-p/384736#M11614</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I'm looking to fully join 7 datasets (set1-set7) together on variable 'ID' to&amp;nbsp;create a master file that contains one record for each ID (no duplicate IDs). &amp;nbsp; The issue is that not all IDs are in every dataset (i.e. ID="333" may only be in set3 and set6). &amp;nbsp; I've been joining the sets piece by piece (see SAS code), &amp;nbsp;but I was curious if anyone knows a way to consolidate my code into one PROC SQL statement&amp;nbsp;to achieve what I'm trying to do. &amp;nbsp;I've attempted myself but I get the SAS NOTE ("&lt;EM&gt;The execution of this query involves performing one or more Cartesian product joins that can not be optimized&lt;/EM&gt;.") &amp;nbsp;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table test1 as 
select distinct coalesce(a.ID, b.ID) as ID, a.var1, b.var2
from 
(select ID, var1 from set1) as a
full join 
(select ID, var2 from set2) as b
on a.ID=b.ID;
quit;
 
proc sql;
create table test2 as 
select distinct coalesce (a.ID, b.ID) as ID, a.var1, a.var2, b.var3
from 
(select ID,var1,var2 from test1) as a
full join
(select ID, var2 from set3) as b
on a.ID=b.ID;
quit;


set 4...
set 5...
set 6...
set 7...&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 01 Aug 2017 19:07:02 GMT</pubDate>
    <dc:creator>glcoolj12</dc:creator>
    <dc:date>2017-08-01T19:07:02Z</dc:date>
    <item>
      <title>FULL JOIN with Multiple Datasets in ID variable (one record per ID)</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/FULL-JOIN-with-Multiple-Datasets-in-ID-variable-one-record-per/m-p/384736#M11614</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I'm looking to fully join 7 datasets (set1-set7) together on variable 'ID' to&amp;nbsp;create a master file that contains one record for each ID (no duplicate IDs). &amp;nbsp; The issue is that not all IDs are in every dataset (i.e. ID="333" may only be in set3 and set6). &amp;nbsp; I've been joining the sets piece by piece (see SAS code), &amp;nbsp;but I was curious if anyone knows a way to consolidate my code into one PROC SQL statement&amp;nbsp;to achieve what I'm trying to do. &amp;nbsp;I've attempted myself but I get the SAS NOTE ("&lt;EM&gt;The execution of this query involves performing one or more Cartesian product joins that can not be optimized&lt;/EM&gt;.") &amp;nbsp;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table test1 as 
select distinct coalesce(a.ID, b.ID) as ID, a.var1, b.var2
from 
(select ID, var1 from set1) as a
full join 
(select ID, var2 from set2) as b
on a.ID=b.ID;
quit;
 
proc sql;
create table test2 as 
select distinct coalesce (a.ID, b.ID) as ID, a.var1, a.var2, b.var3
from 
(select ID,var1,var2 from test1) as a
full join
(select ID, var2 from set3) as b
on a.ID=b.ID;
quit;


set 4...
set 5...
set 6...
set 7...&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 01 Aug 2017 19:07:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/FULL-JOIN-with-Multiple-Datasets-in-ID-variable-one-record-per/m-p/384736#M11614</guid>
      <dc:creator>glcoolj12</dc:creator>
      <dc:date>2017-08-01T19:07:02Z</dc:date>
    </item>
    <item>
      <title>Re: FULL JOIN with Multiple Datasets in ID variable (one record per ID)</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/FULL-JOIN-with-Multiple-Datasets-in-ID-variable-one-record-per/m-p/384774#M11615</link>
      <description>&lt;P&gt;You might consider starting with a set constructed of union joins on select distinct id to create set with only IDs similar to:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data a;
   do id= 1 to 5;
   output;
   end;
run;
data b;
   do id = 3 to 7;
   output;
   end;
run;
data c;
   do id= 6 to 10;
   output;
   end;
run;

proc sql;
   create table Id as
   select Id from a
   union
   select Id from b
   union
   select Id from c;
quit;&lt;/PRE&gt;</description>
      <pubDate>Tue, 01 Aug 2017 19:46:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/FULL-JOIN-with-Multiple-Datasets-in-ID-variable-one-record-per/m-p/384774#M11615</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-08-01T19:46:35Z</dc:date>
    </item>
    <item>
      <title>Re: FULL JOIN with Multiple Datasets in ID variable (one record per ID)</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/FULL-JOIN-with-Multiple-Datasets-in-ID-variable-one-record-per/m-p/384780#M11616</link>
      <description>&lt;P&gt;I like this route, but I have several thousand IDs (that are 8 digits long); moreover, ID is a character variable in my data.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Aug 2017 20:11:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/FULL-JOIN-with-Multiple-Datasets-in-ID-variable-one-record-per/m-p/384780#M11616</guid>
      <dc:creator>glcoolj12</dc:creator>
      <dc:date>2017-08-01T20:11:36Z</dc:date>
    </item>
    <item>
      <title>Re: FULL JOIN with Multiple Datasets in ID variable (one record per ID)</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/FULL-JOIN-with-Multiple-Datasets-in-ID-variable-one-record-per/m-p/384792#M11617</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/132054"&gt;@glcoolj12&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;...but I have several thousand IDs (that are 8 digits long); moreover, ID is a character variable in my data&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;And why exactly would the solution&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;proposed not work for such a situation?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Aug 2017 20:31:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/FULL-JOIN-with-Multiple-Datasets-in-ID-variable-one-record-per/m-p/384792#M11617</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-08-01T20:31:50Z</dc:date>
    </item>
    <item>
      <title>Re: FULL JOIN with Multiple Datasets in ID variable (one record per ID)</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/FULL-JOIN-with-Multiple-Datasets-in-ID-variable-one-record-per/m-p/384826#M11618</link>
      <description>&lt;P&gt;Try the Proc SQL part pointing at your data sets and use the name of your ID variable. This will require the ID variable to have the same&amp;nbsp;name in each set so may need some additional code in each select line if that isn't the case&amp;nbsp;with your actual data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data sets I created, and how,&amp;nbsp;were just to show the syntax and that repeated values of ID did not end up in the results.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Aug 2017 22:13:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/FULL-JOIN-with-Multiple-Datasets-in-ID-variable-one-record-per/m-p/384826#M11618</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-08-01T22:13:07Z</dc:date>
    </item>
    <item>
      <title>Re: FULL JOIN with Multiple Datasets in ID variable (one record per ID)</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/FULL-JOIN-with-Multiple-Datasets-in-ID-variable-one-record-per/m-p/384860#M11619</link>
      <description>Not sure why you would get a cateaian join from a full join.  Perhaps if you could post some sample data?&lt;BR /&gt;If you have no duplicates why the DISTINCT?&lt;BR /&gt;To answer your question, yes you can have all joins in one SQL. And I don't see any need for subqueries that you use in your example.  It you probably need to nest each full join result into the next join so you always join on the current full set of id columns. So in the end,  the example by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; with union is more feasable. &lt;BR /&gt;&lt;BR /&gt;SELECT coalesce(x.id, c.id) as id, x.var1, x.var2, c.var3&lt;BR /&gt;From (&lt;BR /&gt;SELECT coalesce(a.id, b.id,) as id, a.var1, b.var2&lt;BR /&gt;From a&lt;BR /&gt;Full join b&lt;BR /&gt;On a.id =b.id) as x&lt;BR /&gt;Full join c&lt;BR /&gt;On x.id = c.id&lt;BR /&gt;&lt;BR /&gt;And so on...</description>
      <pubDate>Wed, 02 Aug 2017 05:27:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/FULL-JOIN-with-Multiple-Datasets-in-ID-variable-one-record-per/m-p/384860#M11619</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2017-08-02T05:27:05Z</dc:date>
    </item>
    <item>
      <title>Re: FULL JOIN with Multiple Datasets in ID variable (one record per ID)</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/FULL-JOIN-with-Multiple-Datasets-in-ID-variable-one-record-per/m-p/385070#M11627</link>
      <description>When I run the code you suggested, I get an error stating "Ambiguous reference, column "ID" is in more than one table".</description>
      <pubDate>Wed, 02 Aug 2017 18:26:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/FULL-JOIN-with-Multiple-Datasets-in-ID-variable-one-record-per/m-p/385070#M11627</guid>
      <dc:creator>glcoolj12</dc:creator>
      <dc:date>2017-08-02T18:26:52Z</dc:date>
    </item>
    <item>
      <title>Re: FULL JOIN with Multiple Datasets in ID variable (one record per ID)</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/FULL-JOIN-with-Multiple-Datasets-in-ID-variable-one-record-per/m-p/385090#M11629</link>
      <description>&lt;P&gt;Post the code &lt;STRONG&gt;with&lt;/STRONG&gt; the error messages. Use a code box opened with the forum menu icon {i} above to preserve formatting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you get the same error running the example code I posted with the data sets I created?&lt;/P&gt;</description>
      <pubDate>Wed, 02 Aug 2017 19:30:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/FULL-JOIN-with-Multiple-Datasets-in-ID-variable-one-record-per/m-p/385090#M11629</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-08-02T19:30:29Z</dc:date>
    </item>
    <item>
      <title>Re: FULL JOIN with Multiple Datasets in ID variable (one record per ID)</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/FULL-JOIN-with-Multiple-Datasets-in-ID-variable-one-record-per/m-p/385400#M11641</link>
      <description>Thank you everyone for your help! I restructured using the method &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; suggested and was successful. I'm very grateful for all your advice.</description>
      <pubDate>Thu, 03 Aug 2017 17:10:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/FULL-JOIN-with-Multiple-Datasets-in-ID-variable-one-record-per/m-p/385400#M11641</guid>
      <dc:creator>glcoolj12</dc:creator>
      <dc:date>2017-08-03T17:10:41Z</dc:date>
    </item>
  </channel>
</rss>

