<?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 statement giving wrong result in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527701#M143924</link>
    <description>&lt;P&gt;I see. Your suggestion satisfies&lt;/P&gt;
&lt;P&gt;"remove all the year+enrolids combinations which are common in both tables"&lt;/P&gt;
&lt;P&gt;But it also does not satisfy&lt;/P&gt;
&lt;P&gt;"observation count equal to count of table1 - count of table2"&lt;/P&gt;
&lt;P&gt;when you apply it to my example data.&lt;/P&gt;
&lt;P&gt;(both quotes from the original post)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 16 Jan 2019 13:36:24 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-01-16T13:36:24Z</dc:date>
    <item>
      <title>Merge statement giving wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527648#M143900</link>
      <description>&lt;P&gt;I have two tables-&lt;/P&gt;&lt;P&gt;I want to remove all the year+enrolids combinations which are common in both tables. Table1 has more&amp;nbsp;observations than table2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;code-&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data merge;
merge table1( in=a) table2(in=b);
by year enrolid;
if a and not b then output;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now the merge dataset should have observation count equal to count of table1 - count of table2. but that isnt the case.&lt;/P&gt;&lt;P&gt;Is there anything wrong with the code?&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jan 2019 07:47:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527648#M143900</guid>
      <dc:creator>riyaaora275</dc:creator>
      <dc:date>2019-01-16T07:47:36Z</dc:date>
    </item>
    <item>
      <title>Re: Merge statement giving wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527652#M143902</link>
      <description>&lt;P&gt;You most probably have a combination in table2 that is not present in table1, so that will up-end your calculation:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1;
input year enrolid;
cards;
2017 1
2018 1
2019 1
2017 2
2017 3
2018 3
;
run;

data table2;
input year enrolid;
cards;
2019 1
2018 4
;
run;

proc sort data=table1;
by year enrolid;
run;

proc sort data=table2;
by year enrolid;
run;

data merged;
merge table1( in=a) table2(in=b);
by year enrolid;
if a and not b then output;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;since "2018 4" is not present in table one, only one of the observations in table2 results in a delete.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jan 2019 08:02:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527652#M143902</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-01-16T08:02:17Z</dc:date>
    </item>
    <item>
      <title>Re: Merge statement giving wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527694#M143919</link>
      <description>&lt;P&gt;This statement is incorrect if you want to remove the matches:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; a and &lt;SPAN class="token operator"&gt;not&lt;/SPAN&gt; b &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; output&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&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;Instead, use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; a and b &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; delete&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jan 2019 12:56:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527694#M143919</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-01-16T12:56:01Z</dc:date>
    </item>
    <item>
      <title>Re: Merge statement giving wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527696#M143921</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;This statement is incorrect if you want to remove the matches:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; a and &lt;SPAN class="token operator"&gt;not&lt;/SPAN&gt; b &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; output&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead, use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; a and b &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; delete&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;But that would also keep all observations where only b is true.&lt;/P&gt;
&lt;P&gt;If you want to use the delete statement, you'd need&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if a;
if b then delete;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 Jan 2019 13:01:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527696#M143921</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-01-16T13:01:07Z</dc:date>
    </item>
    <item>
      <title>Re: Merge statement giving wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527697#M143922</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I stand by my original post.&amp;nbsp; I know you know this, so perhaps it's time to take a coffee break?&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jan 2019 13:04:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527697#M143922</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-01-16T13:04:30Z</dc:date>
    </item>
    <item>
      <title>Re: Merge statement giving wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527701#M143924</link>
      <description>&lt;P&gt;I see. Your suggestion satisfies&lt;/P&gt;
&lt;P&gt;"remove all the year+enrolids combinations which are common in both tables"&lt;/P&gt;
&lt;P&gt;But it also does not satisfy&lt;/P&gt;
&lt;P&gt;"observation count equal to count of table1 - count of table2"&lt;/P&gt;
&lt;P&gt;when you apply it to my example data.&lt;/P&gt;
&lt;P&gt;(both quotes from the original post)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jan 2019 13:36:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527701#M143924</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-01-16T13:36:24Z</dc:date>
    </item>
    <item>
      <title>Re: Merge statement giving wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527704#M143926</link>
      <description>&lt;P&gt;If only I could write a program that would revise someone's expectations!&amp;nbsp; Alas, I'm limited to writing programs that get the correct result, and then educating to influence the expectations.&amp;nbsp; So far, the issue of mismatches has come up, but there is also the possibility of a many-to-one match.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jan 2019 13:46:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527704#M143926</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-01-16T13:46:32Z</dc:date>
    </item>
    <item>
      <title>Re: Merge statement giving wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527705#M143927</link>
      <description>&lt;P&gt;As always, it boils down to the issue of no available example data against which code can be tested to get the expected results.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jan 2019 13:50:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527705#M143927</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-01-16T13:50:49Z</dc:date>
    </item>
    <item>
      <title>Re: Merge statement giving wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527708#M143928</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; For the benefit of the original poster. With 2 datasets going into the MERGE and understanding that SAS does not really do "MANY-to-MANY merges, you have 5 possible output datasets, as shown below:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="_5_possible_outputs.png" style="width: 561px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/26305iD90EDA82207B89C8/image-size/large?v=v2&amp;amp;px=999" role="button" title="_5_possible_outputs.png" alt="_5_possible_outputs.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Hope this helps put the MERGE in perspective and gives you some ideas about how to fix your MERGE. Without data from you, and a clear explanation of the rules for what you want, it is very hard for others to help you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jan 2019 14:07:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527708#M143928</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2019-01-16T14:07:16Z</dc:date>
    </item>
    <item>
      <title>Re: Merge statement giving wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527716#M143929</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;This statement is incorrect if you want to remove the matches:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; a and &lt;SPAN class="token operator"&gt;not&lt;/SPAN&gt; b &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; output&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead, use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; a and b &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; delete&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Or just use a subsetting IF&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if a^=b;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which can only be true when the observation originates from a single data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jan 2019 14:42:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527716#M143929</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-01-16T14:42:06Z</dc:date>
    </item>
    <item>
      <title>Re: Merge statement giving wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527733#M143939</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/183242"&gt;@riyaaora275&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's look at &lt;EM&gt;one&lt;/EM&gt; BY group, i.e., one YEAR-ENROLID combination which&amp;nbsp;may occur&amp;nbsp;&lt;EM&gt;n&lt;/EM&gt;&amp;nbsp;times in TABLE1 and &lt;EM&gt;k&lt;/EM&gt;&amp;nbsp;times in TABLE2 (&lt;EM&gt;n, k&lt;/EM&gt; &amp;gt;= 0). Using your selection criterion (which looks reasonable), the number of observations with this particular &lt;SPAN&gt;YEAR-ENROLID combination&amp;nbsp;in&amp;nbsp;&lt;/SPAN&gt;the output dataset will be either&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;EM&gt;n&lt;/EM&gt; (if &lt;EM&gt;k =&amp;nbsp;&lt;/EM&gt;0) or&lt;/LI&gt;
&lt;LI&gt;0 (if &lt;EM&gt;k &amp;gt;&amp;nbsp;&lt;/EM&gt;0).&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Now you see that for this generic BY group alone your expected number of observations,&amp;nbsp;&lt;EM&gt;n&amp;nbsp;− k&lt;/EM&gt;, will equal&amp;nbsp;the actual number only&amp;nbsp;if &lt;EM&gt;k =&amp;nbsp;&lt;/EM&gt;0 or &lt;EM&gt;n = k&lt;/EM&gt;. Given that the total number of observations in the output dataset is simply the sum of the numbers in all BY groups, it's clear that this observation count can differ from "&lt;SPAN&gt;count of table1 &lt;EM&gt;−&lt;/EM&gt; count of table2" in both directions. Kurt Bremser has shown an example of the case &lt;EM&gt;n =&lt;/EM&gt; 0, &lt;EM&gt;k =&lt;/EM&gt;&amp;nbsp;1 and Astounding has mentioned the cases &lt;EM&gt;1 = n &amp;lt; k&lt;/EM&gt; and &lt;EM&gt;n &amp;gt; k = 1&lt;/EM&gt;&amp;nbsp;("many-to-one match"). Cynthia's examples cover&amp;nbsp;all possible cases with &lt;EM&gt;n, k &amp;lt;=&lt;/EM&gt; 1.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jan 2019 15:20:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/527733#M143939</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-01-16T15:20:06Z</dc:date>
    </item>
    <item>
      <title>Re: Merge statement giving wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/528244#M144143</link>
      <description>&lt;P&gt;Both of these give the same number of observations.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Jan 2019 05:20:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/528244#M144143</guid>
      <dc:creator>riyaaora275</dc:creator>
      <dc:date>2019-01-18T05:20:43Z</dc:date>
    </item>
    <item>
      <title>Re: Merge statement giving wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/528245#M144144</link>
      <description>You mean I should give some sample data ? Because I cannot give the datafiles.</description>
      <pubDate>Fri, 18 Jan 2019 05:21:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/528245#M144144</guid>
      <dc:creator>riyaaora275</dc:creator>
      <dc:date>2019-01-18T05:21:50Z</dc:date>
    </item>
    <item>
      <title>Re: Merge statement giving wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/528257#M144153</link>
      <description>&lt;P&gt;Creating example data for testing with a data step is an essential SAS skill.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Jan 2019 07:11:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-statement-giving-wrong-result/m-p/528257#M144153</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-01-18T07:11:43Z</dc:date>
    </item>
  </channel>
</rss>

