<?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 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/merge/m-p/917143#M361271</link>
    <description>data long3;&lt;BR /&gt;merge long_run_pd pd_score;&lt;BR /&gt;by segment_id sampdate;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;What about the above, do we take pd from long run pd or pd score, i tested it takes from pd score&lt;BR /&gt;&lt;BR /&gt;What are the rules really?</description>
    <pubDate>Wed, 21 Feb 2024 11:44:49 GMT</pubDate>
    <dc:creator>HeatherNewton</dc:creator>
    <dc:date>2024-02-21T11:44:49Z</dc:date>
    <item>
      <title>merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merge/m-p/917125#M361262</link>
      <description>&lt;PRE&gt;data pd_score;
merge pd_score(in=a) long_run_pd;
by segment_id;
if pd&amp;gt;long_run_pd then long_run_pd=pd;
if a;
run;
&lt;/PRE&gt;
&lt;P&gt;in the above, if segment_id is the same, pd is different in pd_score and long_run_pd, which pd do we get in the resulting table in the merging process?&lt;/P&gt;
&lt;P&gt;also the if then statement, does it matter if it is after the statement if a or before it ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was also looking at the following merge:&lt;/P&gt;
&lt;P&gt;data long3;&lt;/P&gt;
&lt;P&gt;merge long_run_pd pd_score;&lt;/P&gt;
&lt;P&gt;by segment_id sampdate;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;here I am sure if segment_id, sampdate same, it will take pd from sampdate&amp;nbsp; which we can think of as b&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but after this I cant figure out how it works for the other merge with if A anymore...&lt;/P&gt;</description>
      <pubDate>Wed, 21 Feb 2024 10:48:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merge/m-p/917125#M361262</guid>
      <dc:creator>HeatherNewton</dc:creator>
      <dc:date>2024-02-21T10:48:27Z</dc:date>
    </item>
    <item>
      <title>Re: merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merge/m-p/917129#M361265</link>
      <description>&lt;P&gt;Hi to help you better would recommend you go through the following link:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A title="Merge with Caution: How to Avoid Common Problems when Combining SAS Datasets" href="https://support.sas.com/resources/papers/proceedings18/1746-2018.pdf" target="_self"&gt;Merge with Caution&lt;/A&gt;&amp;nbsp; by Joshua M. Horstman&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data pd_score;
merge pd_score(in=a) long_run_pd;
by segment_id;
if pd&amp;gt;long_run_pd then long_run_pd=pd;
if a;
run;&lt;/PRE&gt;
&lt;P&gt;In this merge, if segment_id is the same but pd is different in pd_score and long_run_pd, the pd value from pd_score will be used in the resulting table. This is because pd_score is listed first in the merge statement, so its values have precedence.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The if pd&amp;gt;long_run_pd then long_run_pd=pd; statement is updating long_run_pd to be equal to pd if pd is greater than long_run_pd.&lt;/P&gt;
&lt;P&gt;The if a; statement is a subsetting if statement. It means that only the observations that come from the pd_score dataset (where a is true) will be output to the resulting dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I hope this helps!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Feb 2024 11:08:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merge/m-p/917129#M361265</guid>
      <dc:creator>himself</dc:creator>
      <dc:date>2024-02-21T11:08:36Z</dc:date>
    </item>
    <item>
      <title>Re: merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merge/m-p/917143#M361271</link>
      <description>data long3;&lt;BR /&gt;merge long_run_pd pd_score;&lt;BR /&gt;by segment_id sampdate;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;What about the above, do we take pd from long run pd or pd score, i tested it takes from pd score&lt;BR /&gt;&lt;BR /&gt;What are the rules really?</description>
      <pubDate>Wed, 21 Feb 2024 11:44:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merge/m-p/917143#M361271</guid>
      <dc:creator>HeatherNewton</dc:creator>
      <dc:date>2024-02-21T11:44:49Z</dc:date>
    </item>
    <item>
      <title>Re: merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merge/m-p/917150#M361273</link>
      <description>&lt;P&gt;You could do something like this, which would make it very clear which PD is which&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data pd_score;
    merge pd_score(in=a rename=(pd=pd_from_pd_score)) long_run_pd(rename=(pd=pd_from_long_run));
    by segment_id;
/* More data step commands as necessary */
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Feb 2024 12:34:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merge/m-p/917150#M361273</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-02-21T12:34:41Z</dc:date>
    </item>
    <item>
      <title>Re: merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merge/m-p/917156#M361276</link>
      <description>&lt;P&gt;The answer is actually more complex than you would think.&amp;nbsp; It depends in part on whether you have a one-to-one match or a many-to-one match.&amp;nbsp; If you are interested in reading and learning, here is a link to an old paper than explains the process:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.lexjansen.com/nesug/nesug99/ad/ad155.pdf" target="_blank"&gt;https://www.lexjansen.com/nesug/nesug99/ad/ad155.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Feb 2024 13:18:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merge/m-p/917156#M361276</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2024-02-21T13:18:27Z</dc:date>
    </item>
    <item>
      <title>Re: merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merge/m-p/917280#M361321</link>
      <description>If one to one, which one does it take? I thought would be b</description>
      <pubDate>Thu, 22 Feb 2024 00:04:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merge/m-p/917280#M361321</guid>
      <dc:creator>HeatherNewton</dc:creator>
      <dc:date>2024-02-22T00:04:00Z</dc:date>
    </item>
    <item>
      <title>Re: merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merge/m-p/917286#M361325</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/416388"&gt;@HeatherNewton&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;If one to one, which one does it take? I thought would be b&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please read some of the very good papers already shared with you that explain all of this.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See for example &lt;EM&gt;6. Overlapping variables&lt;/EM&gt; from&amp;nbsp;&lt;A title="Merge with Caution: How to Avoid Common Problems when Combining SAS Datasets" href="https://support.sas.com/resources/papers/proceedings18/1746-2018.pdf" target="_self" rel="nofollow noopener noreferrer"&gt;Merge with Caution&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2024 01:10:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merge/m-p/917286#M361325</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-02-22T01:10:30Z</dc:date>
    </item>
    <item>
      <title>Re: merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merge/m-p/917287#M361326</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/416388"&gt;@HeatherNewton&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;If one to one, which one does it take? I thought would be b&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;No. It value will reflect the last observation read in.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So in a 1 to 1 match if both A and B contribute the the value from B is read last.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if there that value of KEY variables do not exist in B then the value from A stays since nothing was read to overwrite it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2024 01:14:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merge/m-p/917287#M361326</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-02-22T01:14:32Z</dc:date>
    </item>
    <item>
      <title>Re: merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merge/m-p/917291#M361328</link>
      <description>&lt;P&gt;Creating small sample programs to test what's happening often help to clarify things.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
  id=1;var='a1';output;
  id=2;var='a2';output;
run;
data b;
  id=1;var='b1';output;
  id=1;var='b1';output;
  id=3;var='b3';output;
run;

data test;
  merge a b;
  by id;
run;

proc print data=test;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1708565668721.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/93994iBAE873BCED42D36C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1708565668721.png" alt="Patrick_0-1708565668721.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2024 02:58:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merge/m-p/917291#M361328</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-02-22T02:58:36Z</dc:date>
    </item>
  </channel>
</rss>

