Hi,
I included three different variations of solutions because it is unclear why Mary is being excluded. The first version includes Mary, but the latter two do not include Mary. Hope this helps.
hbi
/* 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;
... View more