<?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: proc sql full outer join multiple tables with missing values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-full-outer-join-multiple-tables-with-missing-values/m-p/233167#M42564</link>
    <description>Try specifying it like below:&lt;BR /&gt;&lt;BR /&gt;from tablea&lt;BR /&gt;full join tableb on (join condition)&lt;BR /&gt;full join tablec on (join condition)&lt;BR /&gt;full join tabled on (join condition);&lt;BR /&gt;quit;</description>
    <pubDate>Wed, 04 Nov 2015 20:21:30 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2015-11-04T20:21:30Z</dc:date>
    <item>
      <title>proc sql full outer join multiple tables with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-full-outer-join-multiple-tables-with-missing-values/m-p/233164#M42563</link>
      <description>&lt;P&gt;Hello, I am trying to merge 4 tables and I would like all the final result to contain all of the values from each table,&amp;nbsp;but when I try to do a full outer join it produces way too many results. The data are structured as shown below: table a has a unique id, a zip code, and a name. Table b has a zip code and a city, table c has a zip code and zcta, and table has a zcta and some data.&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;data a; 
	input id zip name $; 
	cards; 
	1 15217 Bob
	2 15217 Sue
	3 15213 Jack
	4 85001 Ginger
	5 91111 Mary
	; 
run; 

data b; 
	input zip city $; 
	cards; 
	15217 Pittsburgh
	15213 Pittsburgh 
	60290 Chicago
	10001 New York 
	85001 Phoenix
	; 
run; 

data c; 
	input zip zcta; 
	cards; 
	15217 15217
	15213 15217
	51331 51331
	60290 60290
	10001 10002
	85001 85001 
; 
run; 

data d; 
	input zcta data $; 
	cards; 
15217 v1
51331 v2
60290 v3
10002 v4
85001 v5
95201 v6
; 
run; 

proc sql; 
	create table outer as 
		select 	a.id, a.name, a.zip, 
				b.city, 
				c.zcta, 
				d.data
		from 			a as a 
		full outer join 
						(b as b, 
						c as c, 
						d as d) 
		on 				a.zip = b.zip 
		and 			a.zip = c.zip
		and 			b.zip = c.zip 
		and 			c.zcta = d.zcta; 
quit; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The result I'd like would be:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	input id name $ zip zcta city $ data $ ; 
	cards; 
	1 Bob 15217 15217 Pittsburgh v1
	2 Sue 15217 15217 Pittsburgh v1
	3 Jack 15213 15217 Pittsburgh v1
	4 Ginger 85001 85001 Phoenix v5
	. . 10001 10002 NewYork v4
	. . 60290 60290 Chicago v3
	. . 51331 51331 . v2
	. . . 95201 . v6
	; 
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is there a straightforward way to do this? Thank you for your help!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Nov 2015 20:15:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-full-outer-join-multiple-tables-with-missing-values/m-p/233164#M42563</guid>
      <dc:creator>shailey</dc:creator>
      <dc:date>2015-11-04T20:15:49Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql full outer join multiple tables with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-full-outer-join-multiple-tables-with-missing-values/m-p/233167#M42564</link>
      <description>Try specifying it like below:&lt;BR /&gt;&lt;BR /&gt;from tablea&lt;BR /&gt;full join tableb on (join condition)&lt;BR /&gt;full join tablec on (join condition)&lt;BR /&gt;full join tabled on (join condition);&lt;BR /&gt;quit;</description>
      <pubDate>Wed, 04 Nov 2015 20:21:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-full-outer-join-multiple-tables-with-missing-values/m-p/233167#M42564</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-11-04T20:21:30Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql full outer join multiple tables with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-full-outer-join-multiple-tables-with-missing-values/m-p/233170#M42565</link>
      <description>&lt;P&gt;Since all of these are only adding one variable, I'd say the easiest is to sort them all by Zip and then use a data step merge.&lt;/P&gt;
&lt;P&gt;Or possibly create formats for ZCTA and whatever that DATA variable stands form . If the ZIP code is supposed to be the code for the City then the column city is not needed with the ZIPCity function available.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Nov 2015 20:26:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-full-outer-join-multiple-tables-with-missing-values/m-p/233170#M42565</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2015-11-04T20:26:41Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql full outer join multiple tables with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-full-outer-join-multiple-tables-with-missing-values/m-p/233171#M42566</link>
      <description>&lt;P&gt;Absolutely fabulous! Thank you so much for your insight.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Nov 2015 20:30:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-full-outer-join-multiple-tables-with-missing-values/m-p/233171#M42566</guid>
      <dc:creator>shailey</dc:creator>
      <dc:date>2015-11-04T20:30:23Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql full outer join multiple tables with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-full-outer-join-multiple-tables-with-missing-values/m-p/233174#M42567</link>
      <description>&lt;P&gt;Thank you for these ideas. Actually, the adding of a single variable each dataset was only for simplicity, there are (many) other variables to merge as well. But I do like the suggestion of using a zip2zcta format&amp;nbsp;to create a new variable in a dataset that contains zip rather than merging using the crosswalk table (table c).&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Nov 2015 20:34:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-full-outer-join-multiple-tables-with-missing-values/m-p/233174#M42567</guid>
      <dc:creator>shailey</dc:creator>
      <dc:date>2015-11-04T20:34:31Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql full outer join multiple tables with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-full-outer-join-multiple-tables-with-missing-values/m-p/233180#M42569</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I included three different variations of&amp;nbsp;solutions because it is unclear why Mary is being excluded. The first version includes Mary, but the latter two do not include Mary.&amp;nbsp;Hope this helps.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;hbi&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;/* this version includes Mary */ 
/* comment: from your example it is unclear why Mary should be excluded */
PROC SQL;
  CREATE TABLE want_version1 AS 
  SELECT a.id
       , a.name
       , COALESCE(a.zip, b.zip, c.zip) as zip
       , b.city
       , COALESCE(c.zcta, d.zcta) as zcta
       , d.data
  FROM a
  FULL OUTER JOIN b ON (b.zip = a.zip)
  FULL OUTER JOIN c ON (c.zip = COALESCE(a.zip, b.zip))
  FULL OUTER JOIN d ON (d.zcta = c.zcta)
  ORDER BY 1, 2, 3, 4, 5, 6;
QUIT;


/* this version excludes Mary */
/* reason for excluding Mary is because: d.zcta is missing */
PROC SQL;
  CREATE TABLE want_version2 AS 
  SELECT a.id
       , a.name
       , COALESCE(a.zip, b.zip, c.zip) as zip
       , b.city
       , COALESCE(c.zcta, d.zcta) as zcta
       , d.data
  FROM a
  FULL OUTER JOIN b ON (b.zip = a.zip)
  FULL OUTER JOIN c ON (c.zip = COALESCE(a.zip, b.zip))
  FULL OUTER JOIN d ON (d.zcta = c.zcta)
  WHERE d.zcta IS NOT MISSING
  ORDER BY 1, 2, 3, 4, 5, 6;
QUIT;


/* this version excludes Mary */
/* reason for excluding Mary is because: d.data is missing */
PROC SQL;
  CREATE TABLE want_version3 AS 
  SELECT a.id
       , a.name
       , COALESCE(a.zip, b.zip, c.zip) as zip
       , b.city
       , COALESCE(c.zcta, d.zcta) as zcta
       , d.data
  FROM a
  FULL OUTER JOIN b ON (b.zip = a.zip)
  FULL OUTER JOIN c ON (c.zip = COALESCE(a.zip, b.zip))
  FULL OUTER JOIN d ON (d.zcta = c.zcta)
  WHERE d.data IS NOT MISSING
  ORDER BY 1, 2, 3, 4, 5, 6;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Nov 2015 20:50:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-full-outer-join-multiple-tables-with-missing-values/m-p/233180#M42569</guid>
      <dc:creator>hbi</dc:creator>
      <dc:date>2015-11-04T20:50:23Z</dc:date>
    </item>
  </channel>
</rss>

