<?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 merging sequence in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/merging-sequence/m-p/904041#M357187</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;example 1
data application;
merge application static dynamic;
by app_ref_no
run;

example 2
data application;
merge application static;
by app_ref_no
run;

data application;
merge application dynamic;
by app_ref_no;
run;

&lt;/PRE&gt;
&lt;P&gt;is example 1 and example 2 going to give same results?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if yes does it apply to&amp;nbsp; more than one variable used for merge&lt;/P&gt;
&lt;P&gt;eg replace in the merge process' by app_ref_no' with 'by app_ref_no org_code;&lt;/P&gt;
&lt;P&gt;etc&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 21 Nov 2023 03:13:50 GMT</pubDate>
    <dc:creator>HeatherNewton</dc:creator>
    <dc:date>2023-11-21T03:13:50Z</dc:date>
    <item>
      <title>merging sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-sequence/m-p/904041#M357187</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;example 1
data application;
merge application static dynamic;
by app_ref_no
run;

example 2
data application;
merge application static;
by app_ref_no
run;

data application;
merge application dynamic;
by app_ref_no;
run;

&lt;/PRE&gt;
&lt;P&gt;is example 1 and example 2 going to give same results?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if yes does it apply to&amp;nbsp; more than one variable used for merge&lt;/P&gt;
&lt;P&gt;eg replace in the merge process' by app_ref_no' with 'by app_ref_no org_code;&lt;/P&gt;
&lt;P&gt;etc&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Nov 2023 03:13:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-sequence/m-p/904041#M357187</guid>
      <dc:creator>HeatherNewton</dc:creator>
      <dc:date>2023-11-21T03:13:50Z</dc:date>
    </item>
    <item>
      <title>Re: merging sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-sequence/m-p/904043#M357188</link>
      <description>&lt;P&gt;&amp;nbsp;I cannot think of a situation where it would produce different results.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why don't you try it yourself and see?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I would create different outputs instead of overwriting one of the inputs.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
  merge application static dynamic;
  by app_ref_no;
run;

data test2_a;
  merge application static;
  by app_ref_no;
run;

data test2;
  merge test2_a dynamic;
  by app_ref_no;
run;



&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that the number of BY variables used to define the BY groups does not matter.&lt;/P&gt;
&lt;P&gt;But if the BY variables need to identify the groupings is different between the datasets then it might make a difference in how you merge them.&lt;/P&gt;
&lt;P&gt;Say you have two key variables&amp;nbsp;app_ref_no and org_code.&amp;nbsp; And some of the datasets have information at the&amp;nbsp;app_ref_no level and others have it at the org_code level.&amp;nbsp; Then you might want to first merge the datasets at the org_code level first and then merge in the app_ref_no datasets so you can use different BY statements for the two merges.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Nov 2023 04:06:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-sequence/m-p/904043#M357188</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-11-21T04:06:17Z</dc:date>
    </item>
    <item>
      <title>Re: merging sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-sequence/m-p/904137#M357219</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;I cannot think of a situation where it would produce different results.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I couldn't think of one either, but creating generic datasets enlightened me: The results are different if&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;There is a variable in dataset DYNAMIC which occurs in either APPLICATION or STATIC, but not both.&lt;/LI&gt;
&lt;LI&gt;The dataset not containing that variable has a BY group with more observations than in DYNAMIC, where the corresponding BY group, in turn, has at least as many observations as in the third dataset, but not zero.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Let n&lt;SUB&gt;a&lt;/SUB&gt;, n&lt;SUB&gt;s&lt;/SUB&gt; and n&lt;SUB&gt;d&lt;/SUB&gt; denote the numbers of observations in the relevant BY groups in&amp;nbsp;APPLICATION, STATIC and DYNAMIC, respectively. Basically, there are four different cases satisfying the above criteria:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Variable not contained in APPLICATION, n&lt;SUB&gt;a&amp;nbsp;&lt;/SUB&gt;&amp;gt; n&lt;SUB&gt;d&lt;/SUB&gt; ≥ n&lt;SUB&gt;s&lt;/SUB&gt;&amp;nbsp;&amp;gt; 0&lt;/LI&gt;
&lt;LI&gt;Variable not contained in APPLICATION, n&lt;SUB&gt;a&amp;nbsp;&lt;/SUB&gt;&amp;gt; n&lt;SUB&gt;d&lt;/SUB&gt;&amp;nbsp;&amp;gt; n&lt;SUB&gt;s&lt;/SUB&gt;&amp;nbsp;= 0&lt;/LI&gt;
&lt;LI&gt;Variable not contained in STATIC, n&lt;SUB&gt;s&amp;nbsp;&lt;/SUB&gt;&amp;gt; n&lt;SUB&gt;d&lt;/SUB&gt; ≥ n&lt;SUB&gt;a&lt;/SUB&gt;&amp;nbsp;&amp;gt; 0&lt;/LI&gt;
&lt;LI&gt;Variable not contained in STATIC, n&lt;SUB&gt;s&amp;nbsp;&lt;/SUB&gt;&amp;gt; n&lt;SUB&gt;d&lt;/SUB&gt;&amp;nbsp;&amp;gt; n&lt;SUB&gt;a&lt;/SUB&gt;&amp;nbsp;= 0&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;The &lt;FONT face="courier new,courier"&gt;app_ref_no&lt;/FONT&gt; BY groups 1, 2, 3, 4 in the sample datasets below represent small examples of these four cases.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data application;
input app_ref_no x :$3.;
cards;
1 ax1
1 ax2
2 ax3
2 ax4
3 ax5
;

data static;
input app_ref_no y :$3.;
cards;
1 sy1
3 sy2
3 sy3
4 sy4
4 sy5
;

data dynamic;
input app_ref_no x :$3. y :$3.;
cards;
1 dx1 dy1
2 dx2 dy2
3 dx3 dy3
4 dx4 dy4
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;P&gt;TEST1 ("triple merge")&lt;FONT face="helvetica"&gt;:&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;Obs    ref_no     x      y

 1        1      dx1    dy1
 2        1      ax2    &lt;STRONG&gt;dy1&lt;/STRONG&gt;
 3        2      dx2    dy2
 4        2      ax4    &lt;STRONG&gt;dy2&lt;/STRONG&gt;
 5        3      dx3    dy3
 6        3      &lt;STRONG&gt;dx3&lt;/STRONG&gt;    sy3
 7        4      dx4    dy4
 8        4      &lt;STRONG&gt;dx4&lt;/STRONG&gt;    sy5&lt;/PRE&gt;
&lt;P&gt;TEST2 ("two-stage merge")&lt;FONT face="helvetica"&gt;:&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;Obs    ref_no     x      y

 1        1      dx1    dy1
 2        1      ax2    &lt;STRONG&gt;sy1&lt;/STRONG&gt;
 3        2      dx2    dy2
 4        2      ax4
 5        3      dx3    dy3
 6        3      &lt;STRONG&gt;ax5&lt;/STRONG&gt;    sy3
 7        4      dx4    dy4
 8        4             sy5&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the "triple merge" the retained value (of the variable in question) from dataset DYNAMIC prevails in the last observation of each BY group because the other two datasets don't contribute any value. In the first step of the "two-stage merge", however, a dataset containing more values (missing or non-missing) than DYNAMIC is created and the surplus values prevail in the second merge step.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Nov 2023 17:25:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-sequence/m-p/904137#M357219</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-11-21T17:25:14Z</dc:date>
    </item>
  </channel>
</rss>

