<?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: Merge VS join in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-VS-join/m-p/301451#M63803</link>
    <description>&lt;P&gt;Thank you for the reply. I see both the methods are correct but which is the best method to use?&lt;/P&gt;</description>
    <pubDate>Thu, 29 Sep 2016 07:26:37 GMT</pubDate>
    <dc:creator>SJN</dc:creator>
    <dc:date>2016-09-29T07:26:37Z</dc:date>
    <item>
      <title>Merge VS join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-VS-join/m-p/301445#M63801</link>
      <description>&lt;P&gt;data a1;&lt;BR /&gt;input no name$;&lt;BR /&gt;cards;&lt;BR /&gt;1 a&lt;BR /&gt;1 b&lt;BR /&gt;1 c&lt;BR /&gt;1 d&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data a2;&lt;BR /&gt;input no name$;&lt;BR /&gt;cards;&lt;BR /&gt;1 e&lt;BR /&gt;1 f&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data DatastepOP;&lt;BR /&gt;merge a1(in=a) a2(in=b);&lt;BR /&gt;by no;&lt;BR /&gt;if a;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table sqlopt&lt;BR /&gt;as&lt;BR /&gt;select a1.*,a2.*&lt;BR /&gt;from a1 left join a2&lt;BR /&gt;on a1.no=a2.no;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;Output:&lt;/P&gt;
&lt;TABLE width="344"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="109"&gt;Data Step Output&lt;/TD&gt;
&lt;TD width="43"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="64"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD colspan="2" width="128"&gt;Proc Sql Output&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="109"&gt;no&lt;/TD&gt;
&lt;TD width="43"&gt;name&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="64"&gt;no&lt;/TD&gt;
&lt;TD width="64"&gt;name&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="109"&gt;1&lt;/TD&gt;
&lt;TD width="43"&gt;e&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="64"&gt;1&lt;/TD&gt;
&lt;TD width="64"&gt;c&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="109"&gt;1&lt;/TD&gt;
&lt;TD width="43"&gt;f&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="64"&gt;1&lt;/TD&gt;
&lt;TD width="64"&gt;a&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="109"&gt;1&lt;/TD&gt;
&lt;TD width="43"&gt;c&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="64"&gt;1&lt;/TD&gt;
&lt;TD width="64"&gt;b&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="109"&gt;1&lt;/TD&gt;
&lt;TD width="43"&gt;d&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="64"&gt;1&lt;/TD&gt;
&lt;TD width="64"&gt;d&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="64"&gt;1&lt;/TD&gt;
&lt;TD width="64"&gt;c&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="64"&gt;1&lt;/TD&gt;
&lt;TD width="64"&gt;a&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="64"&gt;1&lt;/TD&gt;
&lt;TD width="64"&gt;b&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="64"&gt;1&lt;/TD&gt;
&lt;TD width="64"&gt;d&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In both we are doing left join why there is difference in record count?&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2016 06:54:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-VS-join/m-p/301445#M63801</guid>
      <dc:creator>SJN</dc:creator>
      <dc:date>2016-09-29T06:54:25Z</dc:date>
    </item>
    <item>
      <title>Re: Merge VS join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-VS-join/m-p/301450#M63802</link>
      <description>&lt;P&gt;In many-to-many situations, a data step merge behaves different that a SQL join. SQL matches every record from a1 with every record from a2, creating m*n records. Merge works through both datasets sequentially side-by-side, and once there are no new records in one of the datasets, the last one read is kept for merging; so you end up with max(m,n) records. You could better illustrate the behaviour if you named "name" differently in the second dataset a2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your example, the data step&lt;/P&gt;
&lt;P&gt;- reads first record from a1, sets name to "a"&lt;/P&gt;
&lt;P&gt;- reads first record from a2, sets name to "e"&lt;/P&gt;
&lt;P&gt;- outputs and proceeds to next iteration&lt;/P&gt;
&lt;P&gt;- reads second record from a1, sets name to "b"&lt;/P&gt;
&lt;P&gt;- reads second record from a2, sets name to "f"&lt;/P&gt;
&lt;P&gt;- outputs and proceeds to next iteration&lt;/P&gt;
&lt;P&gt;- reads third record from a1, sets name to "c"&lt;/P&gt;
&lt;P&gt;- finds no further record from a2, so does not overwrite name&lt;/P&gt;
&lt;P&gt;- outputs and proceeds to next iteration&lt;/P&gt;
&lt;P&gt;- reads fourth record from a1, sets name to "d"&lt;/P&gt;
&lt;P&gt;- finds no further record from a2, so does not overwrite name&lt;/P&gt;
&lt;P&gt;- outputs and proceeds to next iteration&lt;/P&gt;
&lt;P&gt;- finds no new records, stops&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SQL, OTOH, creates all combinations, but since name is present in both datasets (you will find a nastygram about this in the log), SQL takes only the first occurence; that's why you never get a value from a2.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2016 07:18:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-VS-join/m-p/301450#M63802</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-09-29T07:18:17Z</dc:date>
    </item>
    <item>
      <title>Re: Merge VS join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-VS-join/m-p/301451#M63803</link>
      <description>&lt;P&gt;Thank you for the reply. I see both the methods are correct but which is the best method to use?&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2016 07:26:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-VS-join/m-p/301451#M63803</guid>
      <dc:creator>SJN</dc:creator>
      <dc:date>2016-09-29T07:26:37Z</dc:date>
    </item>
    <item>
      <title>Re: Merge VS join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-VS-join/m-p/301453#M63804</link>
      <description>&lt;P&gt;First of all, I usually prevent a m:n situation when using a datastep merge, as the result is not really predictable; the NOTE in the log about multiple records in both datasets usually means (in my codes) that something went wrong when setting up for the merge.&lt;/P&gt;
&lt;P&gt;Even if I have situation where I have 4 records in dataset a and 2 records in dataset b with identical data (so all resulting records would have the same values from b), I consider that sloppy programming. I'd rather do a sort nodupkey on b first, as that makes my intentions clear in the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have a real m:n situation (meaning none of the contributing datasets have redundant records), the cartesian join that SQL does will usually be correct and desired, as all possible combinations are iterated on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2016 07:36:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-VS-join/m-p/301453#M63804</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-09-29T07:36:55Z</dc:date>
    </item>
    <item>
      <title>Re: Merge VS join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-VS-join/m-p/301455#M63805</link>
      <description>&lt;P&gt;One of the oldest SAS questions.&lt;/P&gt;
&lt;P&gt;Data step works row wise, where SQL works on columns, and performs Cartesian&amp;nbsp;products, and filter by the join criteria.&lt;/P&gt;
&lt;P&gt;I suggest&amp;nbsp;to go through the SAS&amp;nbsp;Language concepts doc, and the SAS&amp;nbsp;programming courses for better understanding.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2016 07:42:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-VS-join/m-p/301455#M63805</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-09-29T07:42:32Z</dc:date>
    </item>
  </channel>
</rss>

