<?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 Join Table on Two Variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Join-Table-on-Two-Variables/m-p/61506#M13363</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Suppose a customer can have multiple customer accounts and purchase_summary table summarizes the purchase totals at customer account level. Not every customer account would have purchase transactions. I use the following query to create a table which includes only customers which had purchase transactions. This query needs to use purchase_summary table twice. I wonder if it is possible to further simplify this query to use purchase_summary once only.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table a1 as&lt;/P&gt;&lt;P&gt;select a.*, b.purchase_total &lt;/P&gt;&lt;P&gt;from customer a left join purchase_summary b&lt;/P&gt;&lt;P&gt;on a.customer_id = b.customer_id&lt;/P&gt;&lt;P&gt;and a.customer_account = b.customer_account&lt;/P&gt;&lt;P&gt;where a.customer_id in (select customer_id from purchase_summary)&lt;/P&gt;&lt;P&gt;; quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 07 Sep 2011 16:17:21 GMT</pubDate>
    <dc:creator>MarcTC</dc:creator>
    <dc:date>2011-09-07T16:17:21Z</dc:date>
    <item>
      <title>Join Table on Two Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-Table-on-Two-Variables/m-p/61506#M13363</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Suppose a customer can have multiple customer accounts and purchase_summary table summarizes the purchase totals at customer account level. Not every customer account would have purchase transactions. I use the following query to create a table which includes only customers which had purchase transactions. This query needs to use purchase_summary table twice. I wonder if it is possible to further simplify this query to use purchase_summary once only.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table a1 as&lt;/P&gt;&lt;P&gt;select a.*, b.purchase_total &lt;/P&gt;&lt;P&gt;from customer a left join purchase_summary b&lt;/P&gt;&lt;P&gt;on a.customer_id = b.customer_id&lt;/P&gt;&lt;P&gt;and a.customer_account = b.customer_account&lt;/P&gt;&lt;P&gt;where a.customer_id in (select customer_id from purchase_summary)&lt;/P&gt;&lt;P&gt;; quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 07 Sep 2011 16:17:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-Table-on-Two-Variables/m-p/61506#M13363</guid>
      <dc:creator>MarcTC</dc:creator>
      <dc:date>2011-09-07T16:17:21Z</dc:date>
    </item>
    <item>
      <title>Join Table on Two Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-Table-on-Two-Variables/m-p/61507#M13364</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If I correctly understand what you want, I don't think your code is doing it.&amp;nbsp; I tried it with the following test files.&lt;/P&gt;&lt;P&gt;Conversely, I think a simple full join does do what you want.&amp;nbsp; Take a look at the following:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/* Some test data */&lt;/P&gt;&lt;P&gt;data customer;&lt;/P&gt;&lt;P&gt;&amp;nbsp; input customer_id customer_account ina;&lt;/P&gt;&lt;P&gt;&amp;nbsp; cards;&lt;/P&gt;&lt;P&gt;1 1 1&lt;/P&gt;&lt;P&gt;1 2 1&lt;/P&gt;&lt;P&gt;2 1 1&lt;/P&gt;&lt;P&gt;3 1 1&lt;/P&gt;&lt;P&gt;4 1 1&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data purchase_summary;&lt;/P&gt;&lt;P&gt;&amp;nbsp; input customer_id customer_account purchase_total;&lt;/P&gt;&lt;P&gt;&amp;nbsp; cards;&lt;/P&gt;&lt;P&gt;1 1 10&lt;/P&gt;&lt;P&gt;3 1 20&lt;/P&gt;&lt;P&gt;4 1 30&lt;/P&gt;&lt;P&gt;5 1 40&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/*what you did */&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table a1 as&lt;/P&gt;&lt;P&gt;select a.*, b.purchase_total&lt;/P&gt;&lt;P&gt;from customer a left join purchase_summary b&lt;/P&gt;&lt;P&gt;on a.customer_id = b.customer_id&lt;/P&gt;&lt;P&gt;and a.customer_account = b.customer_account&lt;/P&gt;&lt;P&gt;where a.customer_id in (select customer_id from purchase_summary)&lt;/P&gt;&lt;P&gt;; quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/*what I think you want to do*/ &lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; create table a2 as&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; select a.*, b.purchase_total&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from customer a, purchase_summary b&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; having a.customer_id = b.customer_id&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and a.customer_account = b.customer_account&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 07 Sep 2011 16:44:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-Table-on-Two-Variables/m-p/61507#M13364</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2011-09-07T16:44:13Z</dc:date>
    </item>
    <item>
      <title>Join Table on Two Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-Table-on-Two-Variables/m-p/61508#M13365</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Pretty sure I am not following the concept of your data but if you provided a better outline of the intended input and output I and others could probably be more helpful.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data purchase_summary;&lt;/P&gt;&lt;P&gt; input customer_account purchase_summary;&lt;/P&gt;&lt;P&gt; cards;&lt;/P&gt;&lt;P&gt;123 100.00&lt;/P&gt;&lt;P&gt;124 100.50&lt;/P&gt;&lt;P&gt;125&amp;nbsp; 99.99&lt;/P&gt;&lt;P&gt;126&amp;nbsp;&amp;nbsp; 1.00&lt;/P&gt;&lt;P&gt;127 999.00&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data customer;&lt;/P&gt;&lt;P&gt; input customer_id customer_account;&lt;/P&gt;&lt;P&gt; cards;&lt;/P&gt;&lt;P&gt;1 123&lt;/P&gt;&lt;P&gt;1 124&lt;/P&gt;&lt;P&gt;2 125&lt;/P&gt;&lt;P&gt;2 126&lt;/P&gt;&lt;P&gt;3 127&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt; create table total as&lt;/P&gt;&lt;P&gt; select a.customer_id, sum(purchase_summary) as purchase_total&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; from customer a, purchase_summary b&lt;/P&gt;&lt;P&gt;&amp;nbsp; where a.customer_account=b.customer_account&lt;/P&gt;&lt;P&gt;&amp;nbsp; group by customer_id;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 07 Sep 2011 16:54:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-Table-on-Two-Variables/m-p/61508#M13365</guid>
      <dc:creator>FriedEgg</dc:creator>
      <dc:date>2011-09-07T16:54:14Z</dc:date>
    </item>
    <item>
      <title>Join Table on Two Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Join-Table-on-Two-Variables/m-p/61509#M13366</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If you only want the ones that are in both datasets then do not use a LEFT JOIN.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; create table a1 as&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; select a.*, b.purchase_total&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; from customer a&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , purchase_summary b&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; where a.customer_id = b.customer_id&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and a.customer_account = b.customer_account&lt;/P&gt;&lt;P&gt;&amp;nbsp; ; &lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Or translated into normal SAS programming:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data a1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; merge customer (in=in1) purchase_total(in=in2 keep=customer_id customer_account purchase_total) ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; by customer_id customer_account ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if in1 and in2;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 07 Sep 2011 17:00:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Join-Table-on-Two-Variables/m-p/61509#M13366</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2011-09-07T17:00:54Z</dc:date>
    </item>
  </channel>
</rss>

