<?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 data step vs SQL in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Merging-data-step-vs-SQL/m-p/539086#M7021</link>
    <description>&lt;P&gt;HI &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/264148"&gt;@Sarah2913&lt;/a&gt;&amp;nbsp; &amp;nbsp;You can replicate if you have rownum for each by group akin to DB sql like oracle or teradata i.e partition by&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data a;
input a;
cards;
1
1
2
2
2
2
2
3
3
;
/*Partition by rownum for each by group*/
data a;
set a;
by a;
if first.a then n=1;
else n+1;
run;


data b;
input a;
cards;
1
1
1
2
3
3
3
;
/*Partition by rownum for each by group*/
data b;
set b;
by a;
if first.a then n1=1;
else n1+1;
run;


data want_merge(drop=n:);
merge a(in=in1) b(in=in2);
by a;
if in1 and in2;
run;

/*replicate merge using many to many to make it one to many*/
proc sql;
create table want_sql(drop=n2) as
select distinct a.a,b.a as b,(max(n,n1)) as n2
from a a inner join b b
on  a.a=b.a;
quit; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Of course the above does involve some logic , nevertheless that's a fun experiment&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 27 Feb 2019 19:00:31 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2019-02-27T19:00:31Z</dc:date>
    <item>
      <title>Merging data step vs SQL</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merging-data-step-vs-SQL/m-p/538696#M6981</link>
      <description>&lt;P&gt;I have the following SAS code&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data merged_data;&lt;/P&gt;&lt;P&gt;merge data1 (in=_1) data2 (in_2) ;&lt;/P&gt;&lt;P&gt;by ID ;&lt;/P&gt;&lt;P&gt;if _1 and _2;&lt;/P&gt;&lt;P&gt;run;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I believe this is similiar to an inner join in SQL but not always the same. Could anyone please explain the difference?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank in advance.&lt;/P&gt;&lt;P&gt;Sarah&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Feb 2019 16:33:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merging-data-step-vs-SQL/m-p/538696#M6981</guid>
      <dc:creator>Sarah2913</dc:creator>
      <dc:date>2019-02-26T16:33:02Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data step vs SQL</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merging-data-step-vs-SQL/m-p/538700#M6982</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data a;
input a;
cards;
1
1
2
2
3
3
;

data b;
input a;
cards;
1
1
2
2
3
3
;

data want;
merge a(in=in1) b(in=in2);
by a;
if in1 and in2;
run;

/*please notice n*m many to many*/
proc sql;
create table w as
select a.a,b.a as b
from a a,b b
where a.a=b.a;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Feb 2019 16:39:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merging-data-step-vs-SQL/m-p/538700#M6982</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-26T16:39:00Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data step vs SQL</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merging-data-step-vs-SQL/m-p/538704#M6984</link>
      <description>&lt;P&gt;So, as long as it is one to many both would mostly likely yield the same results. However many to many makes sql yield different results for the reason it does a cartesian between by groups n*m as opposed to merge that joins by position&lt;/P&gt;</description>
      <pubDate>Tue, 26 Feb 2019 16:42:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merging-data-step-vs-SQL/m-p/538704#M6984</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-26T16:42:08Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data step vs SQL</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merging-data-step-vs-SQL/m-p/538705#M6985</link>
      <description>&lt;P&gt;SAS data step doesn't do a many to many match, whereas SQL does. Otherwise most joins can be replicated with a data step merge.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I generally join using only SQL, more control and easier to understand in my opinion.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/264148"&gt;@Sarah2913&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have the following SAS code&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data merged_data;&lt;/P&gt;
&lt;P&gt;merge data1 (in=_1) data2 (in_2) ;&lt;/P&gt;
&lt;P&gt;by ID ;&lt;/P&gt;
&lt;P&gt;if _1 and _2;&lt;/P&gt;
&lt;P&gt;run;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe this is similiar to an inner join in SQL but not always the same. Could anyone please explain the difference?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank in advance.&lt;/P&gt;
&lt;P&gt;Sarah&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Feb 2019 16:43:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merging-data-step-vs-SQL/m-p/538705#M6985</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-26T16:43:18Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data step vs SQL</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merging-data-step-vs-SQL/m-p/538911#M7000</link>
      <description>Thanks fpr the reply. Is there anyway to repicate the merge exactly using sql?</description>
      <pubDate>Wed, 27 Feb 2019 09:32:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merging-data-step-vs-SQL/m-p/538911#M7000</guid>
      <dc:creator>Sarah2913</dc:creator>
      <dc:date>2019-02-27T09:32:07Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data step vs SQL</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merging-data-step-vs-SQL/m-p/539031#M7012</link>
      <description>&lt;P&gt;I don't know but in my experience, no one ever wants the data to do what the SAS data step is doing.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you're doing straight code conversion for someone, flag this as a possible issue with the model/data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you're doing this as homework, my question would be does this actually occur in the data? Is this an issue you're worried about?&amp;nbsp;&lt;BR /&gt;If you don't have a many to many, but a left or inner join, those can usually be replicated in some form.&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 want as
select a.*, b.*
from have1 as t1
inner join have2 as t2
on t1.ID=t2.ID;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/264148"&gt;@Sarah2913&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thanks fpr the reply. Is there anyway to repicate the merge exactly using sql?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Feb 2019 16:03:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merging-data-step-vs-SQL/m-p/539031#M7012</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-27T16:03:49Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data step vs SQL</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merging-data-step-vs-SQL/m-p/539086#M7021</link>
      <description>&lt;P&gt;HI &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/264148"&gt;@Sarah2913&lt;/a&gt;&amp;nbsp; &amp;nbsp;You can replicate if you have rownum for each by group akin to DB sql like oracle or teradata i.e partition by&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data a;
input a;
cards;
1
1
2
2
2
2
2
3
3
;
/*Partition by rownum for each by group*/
data a;
set a;
by a;
if first.a then n=1;
else n+1;
run;


data b;
input a;
cards;
1
1
1
2
3
3
3
;
/*Partition by rownum for each by group*/
data b;
set b;
by a;
if first.a then n1=1;
else n1+1;
run;


data want_merge(drop=n:);
merge a(in=in1) b(in=in2);
by a;
if in1 and in2;
run;

/*replicate merge using many to many to make it one to many*/
proc sql;
create table want_sql(drop=n2) as
select distinct a.a,b.a as b,(max(n,n1)) as n2
from a a inner join b b
on  a.a=b.a;
quit; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Of course the above does involve some logic , nevertheless that's a fun experiment&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Feb 2019 19:00:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merging-data-step-vs-SQL/m-p/539086#M7021</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-02-27T19:00:31Z</dc:date>
    </item>
  </channel>
</rss>

