<?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: Match merge understanding in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818557#M323103</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;So SAS match merge is like update statement on by fields and it's neither a full join nor inner/left join?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Ignore the SQL concepts of joins.&amp;nbsp; Just process the observations one by one following the rules.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consider each dataset like a face up deck of cards so you can see the value of the next card before you pick it up.&lt;/P&gt;
&lt;P&gt;The MERGE is going to combine the decks so that the ones with the same BY variable values match.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The general process is you look at the top cards in each input and find the "smallest" one.&amp;nbsp; If that starts a new group then erase everything.&amp;nbsp; Take the first card for this new group (scanning the decks from left to right) and read the values from the card into the corresponding variables. If there are top cards from the other decks for this group then read them and update the variables.&amp;nbsp; So if the same variables are coming from two decks (dataset) then the value of the last one read "wins". Once you have scanned across all decks copy the current values to a card and stick into the output deck.&amp;nbsp; Now you are ready to start the next card.&amp;nbsp; Are there any more cards visible on the input decks for this by group?&amp;nbsp; If so again read one card for this group from left to write as before and then write out the result.&amp;nbsp; When there are no more visible cards for this group then clear the variables and find the next lowest group and repeat.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if there is a variable that is only in the second deck and that deck only had one card for this BY group then its value is never changed when the second card for this by group is read from the first deck.&amp;nbsp; That is how a MERGE statement can work add lookup values into a dataset in a many to 1 situation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 16 Jun 2022 12:16:31 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-06-16T12:16:31Z</dc:date>
    <item>
      <title>Match merge understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818536#M323093</link>
      <description>&lt;P&gt;Appreciate if someone of you help me understand this match merge. I want to know&amp;nbsp; in the final result MFGCOMBINE3, why the variables MATRL_NBR and PLANNED_ZNL values are missing in second record and why the value of REASDES got updated in first record.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data MFGCOMBINE1;
	MATRL_NBR=.;
	FISC_YR=2019;
	FISC_PD='012/2019';
	FISC_WK='049/2019';
	PLANT=337;
	MATERIAL2=3010030074;
	PLANNED_ZNL=.;
	REASDES='DO NOT USE Line Performance(LP)';
run;

data MFGCOMBINE2;
	MATRL_NBR=.;
	FISC_YR=2019;
	FISC_PD='012/2019';
	FISC_WK='049/2019';
	PLANT=337;
	MATERIAL2=3010030074;
	PLANNED_ZNL=.;
	REASDES='DO NOT USE Line Performance(LP)';
run;

data MFGCOMBINE;
	set MFGCOMBINE1 MFGCOMBINE2;
run;

data PRIORACTUALS2019A;
	MATRL_NBR=3010030074;
	FISC_YR=2019;
	FISC_PD='012/2019';
	FISC_WK='049/2019';
	PLANT=337;
	MATERIAL2=3010030074;
	PLANNED_ZNL=68478;
	REASDES='Line Performance';
run;

DATA MFGCOMBINE3;
	MERGE WORK.MFGCOMBINE
		WORK.PRIORACTUALS2019A;
	BY FISC_YR FISC_PD FISC_WK PLANT MATERIAL2;
RUN;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Jun 2022 10:59:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818536#M323093</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2022-06-16T10:59:44Z</dc:date>
    </item>
    <item>
      <title>Re: Match merge understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818538#M323094</link>
      <description>&lt;P&gt;So SAS match merge is like update statement on by fields and it's neither a full join nor inner/left join?&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jun 2022 11:18:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818538#M323094</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2022-06-16T11:18:08Z</dc:date>
    </item>
    <item>
      <title>Re: Match merge understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818542#M323095</link>
      <description>&lt;P&gt;reading the documentation may help&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/p1phdzzlrc1wi8n1bpigstwt851o.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/p1phdzzlrc1wi8n1bpigstwt851o.htm&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jun 2022 11:47:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818542#M323095</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2022-06-16T11:47:53Z</dc:date>
    </item>
    <item>
      <title>Re: Match merge understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818547#M323097</link>
      <description>&lt;P&gt;You have a two-to-one merge, with two variables in common that are not part of the BY.&lt;/P&gt;
&lt;P&gt;In the first match, the values from the first dataset are read, then overwritten with the values from the second; for the second match, the values from the second dataset are retained (as no further matching observations are found in the second dataset), and then overwritten with the values from the first dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will do best by not thinking of a data step MERGE as some kind of join, as it isn't (although it can be used for such an operation, with proper diligence). It's also not an update, the data step provides the UPDATE statement for this.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jun 2022 11:52:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818547#M323097</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-06-16T11:52:20Z</dc:date>
    </item>
    <item>
      <title>Re: Match merge understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818548#M323098</link>
      <description>There are multiple examples in the provided link. May I know which example&lt;BR /&gt;should I refer for the program which I shown in my post?&lt;BR /&gt;</description>
      <pubDate>Thu, 16 Jun 2022 11:53:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818548#M323098</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2022-06-16T11:53:14Z</dc:date>
    </item>
    <item>
      <title>Re: Match merge understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818550#M323099</link>
      <description>&lt;P&gt;I specifically point you to this example in &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/181158"&gt;@tarheel13&lt;/a&gt;&amp;nbsp;'s link:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/p1phdzzlrc1wi8n1bpigstwt851o.htm#p1gxq53z4oatain1uyy68hgxsuwd" target="_blank" rel="noopener"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/p1phdzzlrc1wi8n1bpigstwt851o.htm#p1gxq53z4oatain1uyy68hgxsuwd&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;which explicitly covers the mechanics of a one-to-many merge with common variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="3"&gt;&lt;EM&gt;Edit: fixed typo.&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jun 2022 11:57:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818550#M323099</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-06-16T11:57:14Z</dc:date>
    </item>
    <item>
      <title>Re: Match merge understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818551#M323100</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;. I referred to the documentation because the illustrated example explains what happens better than I can &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jun 2022 11:57:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818551#M323100</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2022-06-16T11:57:27Z</dc:date>
    </item>
    <item>
      <title>Re: Match merge understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818557#M323103</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;So SAS match merge is like update statement on by fields and it's neither a full join nor inner/left join?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Ignore the SQL concepts of joins.&amp;nbsp; Just process the observations one by one following the rules.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consider each dataset like a face up deck of cards so you can see the value of the next card before you pick it up.&lt;/P&gt;
&lt;P&gt;The MERGE is going to combine the decks so that the ones with the same BY variable values match.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The general process is you look at the top cards in each input and find the "smallest" one.&amp;nbsp; If that starts a new group then erase everything.&amp;nbsp; Take the first card for this new group (scanning the decks from left to right) and read the values from the card into the corresponding variables. If there are top cards from the other decks for this group then read them and update the variables.&amp;nbsp; So if the same variables are coming from two decks (dataset) then the value of the last one read "wins". Once you have scanned across all decks copy the current values to a card and stick into the output deck.&amp;nbsp; Now you are ready to start the next card.&amp;nbsp; Are there any more cards visible on the input decks for this by group?&amp;nbsp; If so again read one card for this group from left to write as before and then write out the result.&amp;nbsp; When there are no more visible cards for this group then clear the variables and find the next lowest group and repeat.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if there is a variable that is only in the second deck and that deck only had one card for this BY group then its value is never changed when the second card for this by group is read from the first deck.&amp;nbsp; That is how a MERGE statement can work add lookup values into a dataset in a many to 1 situation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jun 2022 12:16:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818557#M323103</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-16T12:16:31Z</dc:date>
    </item>
    <item>
      <title>Re: Match merge understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818560#M323105</link>
      <description>&lt;P&gt;What are you trying to do?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you MERGE two datasets it is normally to add additional VARIABLES from the second dataset to the first dataset.&lt;/P&gt;
&lt;P&gt;In your example the two dataset have the exact same variables.&lt;/P&gt;
&lt;P&gt;So what is it that you are trying to accomplish by merging them?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One situation where it makes sense to have both datasets have the same set of variables (or the second dataset to have a subset of the variables in the first) is when the intent is to apply transactions to the first dataset.&amp;nbsp; &amp;nbsp;If that is what you are trying to do then look at the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p18w3br45er2qun1r8sfmm4grjyr.htm" target="_self"&gt;UPDATE statement&lt;/A&gt;.&amp;nbsp; That will ignore the missing values in the transaction dataset, leaving the values in the original dataset for that by group unchanged.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jun 2022 12:22:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818560#M323105</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-16T12:22:38Z</dc:date>
    </item>
    <item>
      <title>Re: Match merge understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818573#M323109</link>
      <description>&lt;P&gt;Like the SET statement, the MERGE statement is, at its core, based on sequential access of the named datasets, where the sequential processing occurs within matching BY groups of the merged datasets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, here's what happens from &lt;STRONG&gt;MERGE mfgcombine prioractuals2019a;&amp;nbsp; BY ....;&lt;/STRONG&gt; :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;SAS reads the first matching record from mfgcombine.&lt;/LI&gt;
&lt;LI&gt;SAS reads the first matching record (i.e. same BY values) from&amp;nbsp;prioractuals2019a.&amp;nbsp; Any other common variables&amp;nbsp; (&lt;SPAN&gt;MATRL_NBR, PLANNED_ZNL, and REASDES in your case)&amp;nbsp;&lt;/SPAN&gt;will have the values read from mfgcombine overwritten by values from prioractuals2019a.&lt;/LI&gt;
&lt;LI&gt;The first record is output.&amp;nbsp; This produced the results you were presumably expecting.&amp;nbsp; &amp;nbsp;All the variables read in from mfgcombine and&amp;nbsp;prioractuals2019a are not reset to missing after output.&amp;nbsp; They are retained until and unless they are replaced in step 4 or 5 below.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;SAS reads the next matching record (if any) from mfgcombine.&amp;nbsp; If there isn't such a record, the variables named in mfgcombine are not (yet) overwritten, nor set to missing.&amp;nbsp; But in your case there is a second mfgcombine record, which replaces the retained values for&amp;nbsp;&lt;SPAN&gt;MATRL_NBR, PLANNED_ZNL, and REASDES.&amp;nbsp; That is, it replaces the values that were retrieved in step1 and then step 2 (from prioractuals2019a)&amp;nbsp; above.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;SAS finds no second record in prioractuals2019a, so no values were modified.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;SAS outputs the second record.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A couple of notes:&amp;nbsp; Your example contains no variables that belong to just one of the data sets.&amp;nbsp; If it did, then values of those variables would be retained, like all others, from step 3 to step 4.&amp;nbsp; But in your case there is no second record from&amp;nbsp;prioractuals2019a.&amp;nbsp; So any variable present only in the single prioractuals2019a record would be unchanged in all subsequent output records.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also the retain behavior is overwritten when there is a change in any BY variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jun 2022 13:27:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818573#M323109</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-06-16T13:27:44Z</dc:date>
    </item>
    <item>
      <title>Re: Match merge understanding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818601#M323119</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would go with this paper: "How MERGE Really Works" by&amp;nbsp;Bob Virgile&lt;/P&gt;
&lt;P&gt;&lt;A href="https://stats.oarc.ucla.edu/wp-content/uploads/2016/02/ad155.pdf" target="_blank"&gt;https://stats.oarc.ucla.edu/wp-content/uploads/2016/02/ad155.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;It's old but not obsolete, and does what it suppose to do, i.e. explains how MERGE really woks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jun 2022 15:21:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-merge-understanding/m-p/818601#M323119</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2022-06-16T15:21:16Z</dc:date>
    </item>
  </channel>
</rss>

