<?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 sql outer join, limit records from table b in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sql-outer-join-limit-records-from-table-b/m-p/374923#M276415</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;hi,&lt;/P&gt;&lt;P&gt;When joining two tables, I need to see all records from table A, that match&lt;STRONG&gt; only once&lt;/STRONG&gt; to records from table B. In othe words, if table B has 3 obvervations that matches 1 obs from table A, I only want to see 1 'joined' observations (or if you know of another way to arrive at this result).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you in advance. Sample code below&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Data table1;
Input ID $ Account_ID $ Amount;
Datalines;
001 2001 13
002 2002 5
003 2002 4
004 2003 1
Run;
Data table2;
Input ID $ Account_ID $ Amount;
Datalines;
001 2001 13
002 2001 13
003 2001 13
004 2001 13
005 2003 1
Run;

Proc sql; create table GET as
Select  a.*, b.ID AS ID2, B.ACCOUNT_ID AS ACCOUNT_ID2,B. AMOUNT AS AMOUNT2
FROM TABLE1 AS A LEFT JOIN TABLE2 AS B ON
A.ACCOUNT_ID=B.ACCOUNT_ID AND A.AMOUNT=B.AMOUNT;QUIT;

Data WANT;
Input ID $ Account_ID $ Amount ID2 $ ACCOUNT_ID2 $ AMOUNT2 ;
Datalines;
001 2001 13 001 2001 13
002 2002 5  "" ""   null
003 2002 4 "" ""  null
004 2003 1 005 2003 1
Run;&lt;/PRE&gt;&lt;P&gt;(note that the "" in syntax of table Want is meant to appear as blank value)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 11 Jul 2017 12:34:49 GMT</pubDate>
    <dc:creator>brulard</dc:creator>
    <dc:date>2017-07-11T12:34:49Z</dc:date>
    <item>
      <title>sql outer join, limit records from table b</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-outer-join-limit-records-from-table-b/m-p/374923#M276415</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;hi,&lt;/P&gt;&lt;P&gt;When joining two tables, I need to see all records from table A, that match&lt;STRONG&gt; only once&lt;/STRONG&gt; to records from table B. In othe words, if table B has 3 obvervations that matches 1 obs from table A, I only want to see 1 'joined' observations (or if you know of another way to arrive at this result).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you in advance. Sample code below&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Data table1;
Input ID $ Account_ID $ Amount;
Datalines;
001 2001 13
002 2002 5
003 2002 4
004 2003 1
Run;
Data table2;
Input ID $ Account_ID $ Amount;
Datalines;
001 2001 13
002 2001 13
003 2001 13
004 2001 13
005 2003 1
Run;

Proc sql; create table GET as
Select  a.*, b.ID AS ID2, B.ACCOUNT_ID AS ACCOUNT_ID2,B. AMOUNT AS AMOUNT2
FROM TABLE1 AS A LEFT JOIN TABLE2 AS B ON
A.ACCOUNT_ID=B.ACCOUNT_ID AND A.AMOUNT=B.AMOUNT;QUIT;

Data WANT;
Input ID $ Account_ID $ Amount ID2 $ ACCOUNT_ID2 $ AMOUNT2 ;
Datalines;
001 2001 13 001 2001 13
002 2002 5  "" ""   null
003 2002 4 "" ""  null
004 2003 1 005 2003 1
Run;&lt;/PRE&gt;&lt;P&gt;(note that the "" in syntax of table Want is meant to appear as blank value)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jul 2017 12:34:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-outer-join-limit-records-from-table-b/m-p/374923#M276415</guid>
      <dc:creator>brulard</dc:creator>
      <dc:date>2017-07-11T12:34:49Z</dc:date>
    </item>
    <item>
      <title>Re: sql outer join, limit records from table b</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-outer-join-limit-records-from-table-b/m-p/374931#M276416</link>
      <description>&lt;P&gt;Use a data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=table1;
by account_id amount;
run;

proc sort data=table2;
by account_id amount;
run;

data want;
merge
  table1 (in=a)
  table2 (in=b rename=(id=id2))
;
by account_id amount;
if a;
if first.amount;
run;

proc sort data=want;
by id;
run;

proc print data=want noobs;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;       Account_
ID        ID       Amount    id2

001      2001        13      001
002      2002         5         
003      2002         4         
004      2003         1      005
&lt;/PRE&gt;
&lt;P&gt;I omitted account_id2 and amount2, as they are redundant.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jul 2017 12:53:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-outer-join-limit-records-from-table-b/m-p/374931#M276416</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-07-11T12:53:15Z</dc:date>
    </item>
    <item>
      <title>Re: sql outer join, limit records from table b</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-outer-join-limit-records-from-table-b/m-p/374939#M276417</link>
      <description>&lt;P&gt;In Sql you can do this way&lt;/P&gt;
&lt;PRE&gt;Proc sql; 
select a.*, b.* from 
(Select  *
FROM TABLE1)a 
left join
(select min(ID) as ID, account_id, amount
from table2
group by account_id, amount )b
on A.ACCOUNT_ID=B.ACCOUNT_ID AND A.AMOUNT=B.AMOUNT;
QUIT;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Jul 2017 13:22:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-outer-join-limit-records-from-table-b/m-p/374939#M276417</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2017-07-11T13:22:44Z</dc:date>
    </item>
  </channel>
</rss>

