<?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 Difference between Merge and Proc SQL left join in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-Merge-and-Proc-SQL-left-join/m-p/73078#M15733</link>
    <description>I noticed the following difference between the Proc SQl left join and SAS merge. I wanted to be sure whether I am correct or not and if there is a way to do the same thing using Merge.&lt;BR /&gt;
&lt;BR /&gt;
DAtaset1&lt;BR /&gt;
&lt;BR /&gt;
ID                    Post date&lt;BR /&gt;
23456           02Aug2008&lt;BR /&gt;
23456           03Aug2008&lt;BR /&gt;
45678           04Aug2008&lt;BR /&gt;
&lt;BR /&gt;
Dataset2&lt;BR /&gt;
&lt;BR /&gt;
ID                      sell date&lt;BR /&gt;
23456           09Aug2008&lt;BR /&gt;
23456           10Aug2008&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
If we do a left join by Proc sql &lt;BR /&gt;
like this&lt;BR /&gt;
proc sql;&lt;BR /&gt;
create table temp as&lt;BR /&gt;
select t1.*, t2.sell date&lt;BR /&gt;
form dataset1 t1&lt;BR /&gt;
left join&lt;BR /&gt;
dataset2 t2&lt;BR /&gt;
on t1.ID = t2.ID;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
 we will get the following:&lt;BR /&gt;
&lt;BR /&gt;
ID                 Post date              sell date&lt;BR /&gt;
23456            02Aug2008             09Aug2008&lt;BR /&gt;
23456            02Aug2008              10Aug2008&lt;BR /&gt;
23456            03Aug2008             09Aug2008&lt;BR /&gt;
23456            03Aug2008              10Aug2008&lt;BR /&gt;
45678           04Aug2008                  .&lt;BR /&gt;
&lt;BR /&gt;
But if we do a Merge like this&lt;BR /&gt;
data temp;&lt;BR /&gt;
Merge dataset1(in=a) dataset2;&lt;BR /&gt;
if a;&lt;BR /&gt;
by ID;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
we get the following:&lt;BR /&gt;
ID                 Post date              sell date&lt;BR /&gt;
23456            02Aug2008             09Aug2008&lt;BR /&gt;
23456            03Aug2008              10Aug2008&lt;BR /&gt;
45678           04Aug2008                  .&lt;BR /&gt;
&lt;BR /&gt;
Isn't Merge supposed to be equivalent to left join ???&lt;BR /&gt;
&lt;BR /&gt;
Is there any way to do the SQL left join by using data set ?</description>
    <pubDate>Fri, 06 Feb 2009 09:50:45 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2009-02-06T09:50:45Z</dc:date>
    <item>
      <title>Difference between Merge and Proc SQL left join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-Merge-and-Proc-SQL-left-join/m-p/73078#M15733</link>
      <description>I noticed the following difference between the Proc SQl left join and SAS merge. I wanted to be sure whether I am correct or not and if there is a way to do the same thing using Merge.&lt;BR /&gt;
&lt;BR /&gt;
DAtaset1&lt;BR /&gt;
&lt;BR /&gt;
ID                    Post date&lt;BR /&gt;
23456           02Aug2008&lt;BR /&gt;
23456           03Aug2008&lt;BR /&gt;
45678           04Aug2008&lt;BR /&gt;
&lt;BR /&gt;
Dataset2&lt;BR /&gt;
&lt;BR /&gt;
ID                      sell date&lt;BR /&gt;
23456           09Aug2008&lt;BR /&gt;
23456           10Aug2008&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
If we do a left join by Proc sql &lt;BR /&gt;
like this&lt;BR /&gt;
proc sql;&lt;BR /&gt;
create table temp as&lt;BR /&gt;
select t1.*, t2.sell date&lt;BR /&gt;
form dataset1 t1&lt;BR /&gt;
left join&lt;BR /&gt;
dataset2 t2&lt;BR /&gt;
on t1.ID = t2.ID;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
 we will get the following:&lt;BR /&gt;
&lt;BR /&gt;
ID                 Post date              sell date&lt;BR /&gt;
23456            02Aug2008             09Aug2008&lt;BR /&gt;
23456            02Aug2008              10Aug2008&lt;BR /&gt;
23456            03Aug2008             09Aug2008&lt;BR /&gt;
23456            03Aug2008              10Aug2008&lt;BR /&gt;
45678           04Aug2008                  .&lt;BR /&gt;
&lt;BR /&gt;
But if we do a Merge like this&lt;BR /&gt;
data temp;&lt;BR /&gt;
Merge dataset1(in=a) dataset2;&lt;BR /&gt;
if a;&lt;BR /&gt;
by ID;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
we get the following:&lt;BR /&gt;
ID                 Post date              sell date&lt;BR /&gt;
23456            02Aug2008             09Aug2008&lt;BR /&gt;
23456            03Aug2008              10Aug2008&lt;BR /&gt;
45678           04Aug2008                  .&lt;BR /&gt;
&lt;BR /&gt;
Isn't Merge supposed to be equivalent to left join ???&lt;BR /&gt;
&lt;BR /&gt;
Is there any way to do the SQL left join by using data set ?</description>
      <pubDate>Fri, 06 Feb 2009 09:50:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-Merge-and-Proc-SQL-left-join/m-p/73078#M15733</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-02-06T09:50:45Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Merge and Proc SQL left join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-Merge-and-Proc-SQL-left-join/m-p/73079#M15734</link>
      <description>Data step merge has a similar functionality of an outer join, but the result depends on the data. If your relationship between the table keys are 1-M, you will see the same result. In your example, there is a M-M relationship.&lt;BR /&gt;
&lt;BR /&gt;
This is due to the fact that SQL is acting on columns, and the data step has a more row oriented approach.&lt;BR /&gt;
&lt;BR /&gt;
SQL tries to combine all id values between the two tables, and then select the matches (that meet the join criteria). The data step merge by will put the two tables beside each other. As long there is a match in the BY group, the data step  will go to the next row in both tables until there is no match. So, if you have two rows with id=1 in table a, and three rows with id=1 in table two, you will end up with three rows in your output table. Row three will be data from row two in table a, and row three in table b.&lt;BR /&gt;
&lt;BR /&gt;
Hope I could make this any sense...&lt;BR /&gt;
&lt;BR /&gt;
Regards,&lt;BR /&gt;
Linus</description>
      <pubDate>Fri, 06 Feb 2009 10:20:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-Merge-and-Proc-SQL-left-join/m-p/73079#M15734</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2009-02-06T10:20:46Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Merge and Proc SQL left join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-Merge-and-Proc-SQL-left-join/m-p/73080#M15735</link>
      <description>So there is no way to get the desired output (which left join gives) by using data merge ?

Message was edited by: goyal</description>
      <pubDate>Fri, 06 Feb 2009 10:43:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-Merge-and-Proc-SQL-left-join/m-p/73080#M15735</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-02-06T10:43:31Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Merge and Proc SQL left join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-Merge-and-Proc-SQL-left-join/m-p/73081#M15736</link>
      <description>I'm not sure, very difficult in just one step believe. But why? If you know how to do it in SQL, why not stick to that...?&lt;BR /&gt;
&lt;BR /&gt;
/Linus</description>
      <pubDate>Fri, 06 Feb 2009 11:14:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-Merge-and-Proc-SQL-left-join/m-p/73081#M15736</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2009-02-06T11:14:15Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Merge and Proc SQL left join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-Merge-and-Proc-SQL-left-join/m-p/73082#M15737</link>
      <description>data step merge does not correspond to a cartesian-type (m-n) join.  &lt;BR /&gt;
 &lt;BR /&gt;
That type of join be achieved in a data step if that is your preference, by using two set statements and controlling the reads from each table, probably with key= indexed access on at least one of these tables. &lt;BR /&gt;
I don't need that data step complexity because I think the sql join is easier to recognise, understand and therefore, maintain. Why would you seek it?&lt;BR /&gt;
&lt;BR /&gt;
PeterC</description>
      <pubDate>Mon, 09 Feb 2009 09:07:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-Merge-and-Proc-SQL-left-join/m-p/73082#M15737</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-02-09T09:07:42Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Merge and Proc SQL left join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-Merge-and-Proc-SQL-left-join/m-p/73083#M15738</link>
      <description>I am seeking this because I thought it would be possible to do it by a simple Merge statement. It was quite surprising that left join and  data merge are not equivalent.</description>
      <pubDate>Mon, 09 Feb 2009 09:37:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-Merge-and-Proc-SQL-left-join/m-p/73083#M15738</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-02-09T09:37:02Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between Merge and Proc SQL left join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-Merge-and-Proc-SQL-left-join/m-p/73084#M15739</link>
      <description>MERGE will happily provide 1-N and N-1 joins (but with unexpected effects, in the nature of a data step, when variables are common to both tables but not among the list of by- variables).&lt;BR /&gt;
It looked to me like you wanted a M-N join, because you have repeats of the by-variable on both data sets.&lt;BR /&gt;
When by-values repeat on more than one table in a MERGE, the effect is like two diaries side-by-side. Each time a MERGE statement is executed a row of data is taken from each table in the statement until there is no more data for that by-value in any table on the statement. I expect it is better described in the proper documentation.&lt;BR /&gt;
 &lt;BR /&gt;
good luck&lt;BR /&gt;
 &lt;BR /&gt;
&lt;BR /&gt;
PeterC</description>
      <pubDate>Mon, 09 Feb 2009 10:07:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-Merge-and-Proc-SQL-left-join/m-p/73084#M15739</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-02-09T10:07:36Z</dc:date>
    </item>
  </channel>
</rss>

