<?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: Merging/Joining Datasets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merging-Joining-Datasets/m-p/390978#M93846</link>
    <description>&lt;P&gt;This is a many to many join, which ends up with 2X2 -&amp;gt; 4 records which is not what you want.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would suggest a data step merge instead.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 25 Aug 2017 16:59:43 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2017-08-25T16:59:43Z</dc:date>
    <item>
      <title>Merging/Joining Datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Joining-Datasets/m-p/390968#M93844</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to Merge the two data sets&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data example1;
Input company$ employee age amount prob month;
datalines;
ABC 01 24 50000 90 5
ABC 01 24 0 10 7
;
data example2;
Input company$ employee age amount prob month;
datalines;
ABC 03 25 21000 90 5
ABC 03 25 0 10 7
;
run;

Proc sql;
create table combined as
select a.company,a.employee,a.amount as ex1_amount,b.amount as ex2_amount,a.prob,a.month
from example1 as a left join example2 as b on a.company=b.company;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Expected Output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ABC 1 50000 21000 90 5&lt;/P&gt;&lt;P&gt;ABC 1 0 0 10 7&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2017 16:26:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Joining-Datasets/m-p/390968#M93844</guid>
      <dc:creator>Harmandeep</dc:creator>
      <dc:date>2017-08-25T16:26:36Z</dc:date>
    </item>
    <item>
      <title>Re: Merging/Joining Datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Joining-Datasets/m-p/390975#M93845</link>
      <description>&lt;P&gt;Your expectations about SQL are incorrect. &amp;nbsp;SQL does not match observation by observation. &amp;nbsp;Instead, it finds all matches, so you will get:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First ABC from EXAMPLE1, matched with first ABC from EXAMPLE2&lt;/P&gt;
&lt;P&gt;First ABC from EXAMPLE1, matched with second ABC from EXAMPLE2&lt;/P&gt;
&lt;P&gt;Second ABC from EXAMPLE1, matched with first ABC from EXAMPLE2&lt;/P&gt;
&lt;P&gt;Second ABC from EXAMPLE1, matched with second ABC from EXAMPLE2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SQL is just the wrong tool for the job, to produce your expected output.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2017 16:46:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Joining-Datasets/m-p/390975#M93845</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-08-25T16:46:31Z</dc:date>
    </item>
    <item>
      <title>Re: Merging/Joining Datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Joining-Datasets/m-p/390978#M93846</link>
      <description>&lt;P&gt;This is a many to many join, which ends up with 2X2 -&amp;gt; 4 records which is not what you want.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would suggest a data step merge instead.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2017 16:59:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Joining-Datasets/m-p/390978#M93846</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-08-25T16:59:43Z</dc:date>
    </item>
    <item>
      <title>Re: Merging/Joining Datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Joining-Datasets/m-p/390979#M93847</link>
      <description>&lt;P&gt;May be you want to explicitly say that the data should also match on month?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Proc sql;
   create table combined as
   select a.company,a.employee,a.amount as ex1_amount, b.amount as ex2_amount,a.prob,a.month
   from example1 as a left join example2 as b 
        on    a.company =b.company
          and a.month   =b.month
 ;
quit;&lt;/PRE&gt;
&lt;P&gt;Though I have sneeking feeling that since you are ignoring the employee value that something else is going on and you are possibly taking a complex approach to transposition&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2017 17:09:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Joining-Datasets/m-p/390979#M93847</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-08-25T17:09:50Z</dc:date>
    </item>
    <item>
      <title>Re: Merging/Joining Datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-Joining-Datasets/m-p/391016#M93856</link>
      <description>&lt;P&gt;The other responders have answered your question, but just to help with your knowledge I'm going to add this, which I've used with a number of colleagues over the years.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A SQL join starts out (in the case of a two table join) by matching every record with the first table to every record from the second table. So, as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt; and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; explain in the case of two tables with two records each, you start out with 2 x 2 or 4 result records.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are actually cases where this is useful, but they are rare. So to get the desired results, you use other SQL clauses to "trim away" the result records you don't want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first is the left / right / inner join phrasing, accompanied by an "on" clause, where you're telling SQL that out of the enormous number of result records, ONLY keep the ones where there's a record in the left or the right table, or only the records where a field from the left table matches a field from the right table. This usually reduces the number of result records from a x b to the record count of a or b, or less depending on matching, which is usually more in line with what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once SQL has reduced the number of records based on your join logic, you can then reduce it even further using the "where" clause, which tells SQL to additionally only keep the records from the first part that match the conditions that you specify.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, in conclusion, I always consider a SQL join as creating a massive result set, which I then trim away using the different language options.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;BR /&gt;&amp;nbsp;&amp;nbsp; Tom&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2017 20:34:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-Joining-Datasets/m-p/391016#M93856</guid>
      <dc:creator>TomKari</dc:creator>
      <dc:date>2017-08-25T20:34:21Z</dc:date>
    </item>
  </channel>
</rss>

