<?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 giving wrong result in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-giving-wrong-result/m-p/528264#M144158</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/183242"&gt;@riyaaora275&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tst1;
input a $ yr  b;
datalines ;
a 2000 1
b 2000 2
b 2003 3
c 1996 4
d 2013 3
;
run;

data tst2;
input a $ yr  c;
datalines ;
a 2000 10
b 2003 13
d 2000 13
;
run;


proc sort data = tst1 ; by   a;run;
proc sort data = tst2 ; by  a;run;
data tst3;
merge tst1 (in=x) tst2 (in=y);
by a;
if x  then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I run this the result is&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;a&amp;nbsp; &amp;nbsp;yr&amp;nbsp; &amp;nbsp; &amp;nbsp;b&amp;nbsp; &amp;nbsp;c&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;a 2000 1 10&lt;/P&gt;
&lt;P&gt;b&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;2003&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;2 13&lt;/P&gt;
&lt;P&gt;b&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;2003&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;3 13&lt;/P&gt;
&lt;P&gt;c 1996 4&amp;nbsp; .&lt;/P&gt;
&lt;P&gt;d 2000 3 13&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why am I&amp;nbsp; getting the same year value for b? year corresponding to b =2 in tst1 is 2000&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You do &lt;FONT size="5"&gt;&lt;STRONG&gt;NOT&lt;/STRONG&gt;&lt;/FONT&gt; merge datasets that have variables in common other than those in the by statement.&lt;/P&gt;
&lt;P&gt;When encountering the value "b" for variable a, this happens:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The PDV is filled with data from tst1, and looks like this:&lt;BR /&gt;b 2000 2&lt;/LI&gt;
&lt;LI&gt;the matching observation from dataset tst2 is read, and &lt;U&gt;yr is overwritten&lt;/U&gt;:&lt;BR /&gt;b 2003 2 13&lt;/LI&gt;
&lt;LI&gt;the PDV is written to tst3&lt;/LI&gt;
&lt;LI&gt;the next observation from tst1 is read, and the PDV looks like this:&lt;BR /&gt;b 2003 3 13&lt;BR /&gt;Note that 2003 &lt;U&gt;is now the value from tst1&lt;/U&gt;, and variable c is retained (all variables from input datasets are automatically retained)&lt;/LI&gt;
&lt;LI&gt;since no further matching observations are present in tst2, no read is done there&lt;/LI&gt;
&lt;LI&gt;the PDV is written to tst3&lt;/LI&gt;
&lt;/UL&gt;</description>
    <pubDate>Fri, 18 Jan 2019 08:18:58 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-01-18T08:18:58Z</dc:date>
    <item>
      <title>Merge giving wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-giving-wrong-result/m-p/528252#M144149</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tst1;
input a $ yr  b;
datalines ;
a 2000 1
b 2000 2
b 2003 3
c 1996 4
d 2013 3
;
run;

data tst2;
input a $ yr  c;
datalines ;
a 2000 10
b 2003 13
d 2000 13
;
run;


proc sort data = tst1 ; by   a;run;
proc sort data = tst2 ; by  a;run;
data tst3;
merge tst1 (in=x) tst2 (in=y);
by a;
if x  then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I run this the result is&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;a&amp;nbsp; &amp;nbsp;yr&amp;nbsp; &amp;nbsp; &amp;nbsp;b&amp;nbsp; &amp;nbsp;c&lt;/U&gt;&lt;/P&gt;&lt;P&gt;a 2000 1 10&lt;/P&gt;&lt;P&gt;b&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;2003&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;2 13&lt;/P&gt;&lt;P&gt;b&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;2003&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;3 13&lt;/P&gt;&lt;P&gt;c 1996 4&amp;nbsp; .&lt;/P&gt;&lt;P&gt;d 2000 3 13&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Why am I&amp;nbsp; getting the same year value for b? year corresponding to b =2 in tst1 is 2000&lt;/P&gt;</description>
      <pubDate>Fri, 18 Jan 2019 06:28:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-giving-wrong-result/m-p/528252#M144149</guid>
      <dc:creator>riyaaora275</dc:creator>
      <dc:date>2019-01-18T06:28:53Z</dc:date>
    </item>
    <item>
      <title>Re: Merge giving wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-giving-wrong-result/m-p/528254#M144150</link>
      <description>&lt;P&gt;Because you only have yr=2003 for b=2 in tst2. I think this gives you your desired result?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tst1;
input a $ yr  b;
datalines ;
a 2000 1
b 2000 2
b 2003 3
c 1996 4
d 2013 3
;
run;

data tst2;
input a $ yr  c;
datalines ;
a 2000 10
b 2003 13
d 2000 13
;
run;


proc sort data = tst1 ; by a yr; run;
proc sort data = tst2 ; by a yr; run;
data tst3;
merge tst1 (in=x) tst2 (in=y);
by a yr;
if x then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Jan 2019 07:01:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-giving-wrong-result/m-p/528254#M144150</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-01-18T07:01:55Z</dc:date>
    </item>
    <item>
      <title>Re: Merge giving wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-giving-wrong-result/m-p/528256#M144152</link>
      <description>Google and read an old paper: How Merge Really Works</description>
      <pubDate>Fri, 18 Jan 2019 07:11:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-giving-wrong-result/m-p/528256#M144152</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-01-18T07:11:42Z</dc:date>
    </item>
    <item>
      <title>Re: Merge giving wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-giving-wrong-result/m-p/528264#M144158</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/183242"&gt;@riyaaora275&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tst1;
input a $ yr  b;
datalines ;
a 2000 1
b 2000 2
b 2003 3
c 1996 4
d 2013 3
;
run;

data tst2;
input a $ yr  c;
datalines ;
a 2000 10
b 2003 13
d 2000 13
;
run;


proc sort data = tst1 ; by   a;run;
proc sort data = tst2 ; by  a;run;
data tst3;
merge tst1 (in=x) tst2 (in=y);
by a;
if x  then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I run this the result is&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;a&amp;nbsp; &amp;nbsp;yr&amp;nbsp; &amp;nbsp; &amp;nbsp;b&amp;nbsp; &amp;nbsp;c&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;a 2000 1 10&lt;/P&gt;
&lt;P&gt;b&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;2003&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;2 13&lt;/P&gt;
&lt;P&gt;b&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;2003&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;3 13&lt;/P&gt;
&lt;P&gt;c 1996 4&amp;nbsp; .&lt;/P&gt;
&lt;P&gt;d 2000 3 13&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why am I&amp;nbsp; getting the same year value for b? year corresponding to b =2 in tst1 is 2000&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You do &lt;FONT size="5"&gt;&lt;STRONG&gt;NOT&lt;/STRONG&gt;&lt;/FONT&gt; merge datasets that have variables in common other than those in the by statement.&lt;/P&gt;
&lt;P&gt;When encountering the value "b" for variable a, this happens:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The PDV is filled with data from tst1, and looks like this:&lt;BR /&gt;b 2000 2&lt;/LI&gt;
&lt;LI&gt;the matching observation from dataset tst2 is read, and &lt;U&gt;yr is overwritten&lt;/U&gt;:&lt;BR /&gt;b 2003 2 13&lt;/LI&gt;
&lt;LI&gt;the PDV is written to tst3&lt;/LI&gt;
&lt;LI&gt;the next observation from tst1 is read, and the PDV looks like this:&lt;BR /&gt;b 2003 3 13&lt;BR /&gt;Note that 2003 &lt;U&gt;is now the value from tst1&lt;/U&gt;, and variable c is retained (all variables from input datasets are automatically retained)&lt;/LI&gt;
&lt;LI&gt;since no further matching observations are present in tst2, no read is done there&lt;/LI&gt;
&lt;LI&gt;the PDV is written to tst3&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Fri, 18 Jan 2019 08:18:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-giving-wrong-result/m-p/528264#M144158</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-01-18T08:18:58Z</dc:date>
    </item>
    <item>
      <title>Re: Merge giving wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-giving-wrong-result/m-p/528275#M144164</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/183242"&gt;@riyaaora275&lt;/a&gt;: I recommend setting the &lt;A href="https://documentation.sas.com/?docsetId=lesysoptsref&amp;amp;docsetTarget=p17071zptzpruyn1a5hirqyle3r3.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank"&gt;MSGLEVEL=&lt;/A&gt; system option to I (e.g. in your autoexec file):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options msglevel=I;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you'll get messages in the log such as&lt;/P&gt;
&lt;PRE&gt;INFO: The variable yr on data set WORK.TST1 will be overwritten by data set WORK.TST2.&lt;/PRE&gt;
&lt;P&gt;which will help you to understand&amp;nbsp;the results in situations like this.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Jan 2019 09:20:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-giving-wrong-result/m-p/528275#M144164</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-01-18T09:20:47Z</dc:date>
    </item>
    <item>
      <title>Re: Merge giving wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-giving-wrong-result/m-p/528276#M144165</link>
      <description>Thanks a lot.</description>
      <pubDate>Fri, 18 Jan 2019 09:25:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-giving-wrong-result/m-p/528276#M144165</guid>
      <dc:creator>riyaaora275</dc:creator>
      <dc:date>2019-01-18T09:25:29Z</dc:date>
    </item>
  </channel>
</rss>

