<?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: Left Join Count of Records with Proc SQL Producing Duplicate Records in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Left-Join-Count-of-Records-with-Proc-SQL-Producing-Duplicate/m-p/581901#M13762</link>
    <description>&lt;P&gt;If you only want one row per customer, what invoice date do you want to read - the earliest or latest? If you want the latest this should work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table full as
select  a.user_id 
       ,B.*
from customers a
LEFT JOIN 
( select user_id
         ,count(invoices) as invoices
         ,max(invoice_date) as latest_invoice_date
  from invoice_db
  where invoice_date&amp;gt;='01JAN2019'd
  GROUP BY user_id
) as B
on a.user_id=b.user_id
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 17 Aug 2019 01:04:27 GMT</pubDate>
    <dc:creator>SASKiwi</dc:creator>
    <dc:date>2019-08-17T01:04:27Z</dc:date>
    <item>
      <title>Left Join Count of Records with Proc SQL Producing Duplicate Records</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Left-Join-Count-of-Records-with-Proc-SQL-Producing-Duplicate/m-p/581898#M13761</link>
      <description>&lt;P&gt;I have the following two tables that I want to left join using Proc SQL. One is a table of user ID's, the other are all the sales invoices.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I want is to join the invoices table onto the user ID table so that the count of invoices shows up in an "invoices" column. My problem is that the user ID's become duplicated.&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 full as
select count(b.invoices) as invoices, b.user_id, b.invoice_date, a.user_id from customers a
LEFT JOIN invoice_db b
on a.user_id=b.user_id
where invoice_date&amp;gt;='01JAN2019'd
GROUP BY a.user_id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The output looks like&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1	1607
2	1638
2	1638
1	1665
8	2434
8	2434&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I need is&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1	1607
2	1638
1	1665
8	2434&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Aug 2019 23:48:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Left-Join-Count-of-Records-with-Proc-SQL-Producing-Duplicate/m-p/581898#M13761</guid>
      <dc:creator>anonymous_user</dc:creator>
      <dc:date>2019-08-16T23:48:43Z</dc:date>
    </item>
    <item>
      <title>Re: Left Join Count of Records with Proc SQL Producing Duplicate Records</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Left-Join-Count-of-Records-with-Proc-SQL-Producing-Duplicate/m-p/581901#M13762</link>
      <description>&lt;P&gt;If you only want one row per customer, what invoice date do you want to read - the earliest or latest? If you want the latest this should work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table full as
select  a.user_id 
       ,B.*
from customers a
LEFT JOIN 
( select user_id
         ,count(invoices) as invoices
         ,max(invoice_date) as latest_invoice_date
  from invoice_db
  where invoice_date&amp;gt;='01JAN2019'd
  GROUP BY user_id
) as B
on a.user_id=b.user_id
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 17 Aug 2019 01:04:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Left-Join-Count-of-Records-with-Proc-SQL-Producing-Duplicate/m-p/581901#M13762</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2019-08-17T01:04:27Z</dc:date>
    </item>
  </channel>
</rss>

