<?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: Simple Data Merge question in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Simple-Data-Merge-question/m-p/289860#M59895</link>
    <description>&lt;P&gt;If there's no need to match on ID, a simplified version of an already-suggested solution would work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data final;&lt;/P&gt;
&lt;P&gt;set d1;&lt;/P&gt;
&lt;P&gt;set d2;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you do need to match on ID, the program is more complex:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data final;&lt;/P&gt;
&lt;P&gt;from_d1=0;&lt;/P&gt;
&lt;P&gt;from_d2=0;&lt;/P&gt;
&lt;P&gt;merge d1 (in=from_d1) d2 (in=from_db);&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;if from_d1 and from_d2;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
    <pubDate>Fri, 05 Aug 2016 17:59:09 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2016-08-05T17:59:09Z</dc:date>
    <item>
      <title>Simple Data Merge question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simple-Data-Merge-question/m-p/289839#M59886</link>
      <description>&lt;P&gt;I have to merge 2 datasets as below and need only number of rows which are in Dataset 2 (2 in this case)&lt;/P&gt;&lt;P&gt;Keys are column ID and B.&lt;/P&gt;&lt;P&gt;When i merge it gives me 3 rows&amp;nbsp;as a result of many to many merge.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data final;&lt;/P&gt;&lt;P&gt;&amp;nbsp;merge D1 (in=a) D2(in=b);&lt;/P&gt;&lt;P&gt;if b;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dataset 1&lt;/P&gt;&lt;P&gt;ID &amp;nbsp; B &amp;nbsp; C&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp;51 &amp;nbsp;bb&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp;51 &amp;nbsp;bb&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp;51 &amp;nbsp;bb&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dataset 2&lt;/P&gt;&lt;P&gt;ID &amp;nbsp; B &amp;nbsp; D&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp;51 &amp;nbsp;aa&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp;51 &amp;nbsp;aa&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what i need in result.&lt;/P&gt;&lt;P&gt;Final&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;ID&lt;/SPAN&gt;&lt;SPAN&gt; &amp;nbsp; B &amp;nbsp; C &amp;nbsp; D&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp;51 &amp;nbsp;bb aa&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp;51 &amp;nbsp;bb aa&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>Fri, 05 Aug 2016 16:49:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simple-Data-Merge-question/m-p/289839#M59886</guid>
      <dc:creator>deepanshub</dc:creator>
      <dc:date>2016-08-05T16:49:28Z</dc:date>
    </item>
    <item>
      <title>Re: Simple Data Merge question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simple-Data-Merge-question/m-p/289843#M59887</link>
      <description>&lt;P&gt;Try:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data final;&lt;/P&gt;&lt;P&gt;&amp;nbsp;merge D1 (in=a) D2(in=k);&lt;/P&gt;&lt;P&gt;if k;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Aug 2016 16:57:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simple-Data-Merge-question/m-p/289843#M59887</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2016-08-05T16:57:33Z</dc:date>
    </item>
    <item>
      <title>Re: Simple Data Merge question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simple-Data-Merge-question/m-p/289844#M59888</link>
      <description>My 2 cents:&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data final;&lt;BR /&gt;set D1;&lt;BR /&gt;set D2;&lt;BR /&gt;by B;&lt;BR /&gt;run;&lt;BR /&gt;</description>
      <pubDate>Fri, 05 Aug 2016 17:01:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simple-Data-Merge-question/m-p/289844#M59888</guid>
      <dc:creator>jj02148</dc:creator>
      <dc:date>2016-08-05T17:01:03Z</dc:date>
    </item>
    <item>
      <title>Re: Simple Data Merge question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simple-Data-Merge-question/m-p/289845#M59889</link>
      <description>&lt;P&gt;You may have been getting problems because of the use of in=B when you have a variable B.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For your example data this works:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT face="SAS Monospace" size="2"&gt;&lt;FONT face="SAS Monospace" size="2"&gt;&lt;CODE class=" language-sas"&gt;Data d1;
   input ID   B   C $;
datalines;
1    51  bb
1    51  bb
1    52  bb
;
Data d2;
   input ID   B   D  $;
datalines;
1    51  aa
1    51  aa
;
run;

Data final;
   merge D1 (in=d1) D2(in=d2);
   by id b;
   if d2;
run;
&lt;/CODE&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I suspect you may have inteded to use a BY statement to match on ID and B&lt;/P&gt;</description>
      <pubDate>Fri, 05 Aug 2016 17:01:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simple-Data-Merge-question/m-p/289845#M59889</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-08-05T17:01:18Z</dc:date>
    </item>
    <item>
      <title>Re: Simple Data Merge question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simple-Data-Merge-question/m-p/289860#M59895</link>
      <description>&lt;P&gt;If there's no need to match on ID, a simplified version of an already-suggested solution would work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data final;&lt;/P&gt;
&lt;P&gt;set d1;&lt;/P&gt;
&lt;P&gt;set d2;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you do need to match on ID, the program is more complex:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data final;&lt;/P&gt;
&lt;P&gt;from_d1=0;&lt;/P&gt;
&lt;P&gt;from_d2=0;&lt;/P&gt;
&lt;P&gt;merge d1 (in=from_d1) d2 (in=from_db);&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;if from_d1 and from_d2;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Aug 2016 17:59:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simple-Data-Merge-question/m-p/289860#M59895</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-08-05T17:59:09Z</dc:date>
    </item>
    <item>
      <title>Re: Simple Data Merge question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simple-Data-Merge-question/m-p/289866#M59896</link>
      <description>&lt;P&gt;It returns 3 rows instead of 2 rows i need from dataset 2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Aug 2016 18:53:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simple-Data-Merge-question/m-p/289866#M59896</guid>
      <dc:creator>deepanshub</dc:creator>
      <dc:date>2016-08-05T18:53:34Z</dc:date>
    </item>
    <item>
      <title>Re: Simple Data Merge question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simple-Data-Merge-question/m-p/289868#M59897</link>
      <description>&lt;P&gt;Thank you so much.. this works.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am relatively new to SAS..would you mind sharing some more details of how initializing the tracking variables to 0 actually gives us the results needed here. Appreciate your help.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Aug 2016 19:03:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simple-Data-Merge-question/m-p/289868#M59897</guid>
      <dc:creator>deepanshub</dc:creator>
      <dc:date>2016-08-05T19:03:52Z</dc:date>
    </item>
    <item>
      <title>Re: Simple Data Merge question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simple-Data-Merge-question/m-p/289871#M59899</link>
      <description>&lt;P&gt;There's actually a two-part answer, none of which is particularly simple.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(1) When does SAS read a record from each source of data&lt;/P&gt;
&lt;P&gt;(2) When does SAS set the in= variables to 0 or 1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second question is the easier one. &amp;nbsp;SAS sets both in= variables to 0 when beginning a new value for the BY variable. &amp;nbsp;If a record gets read in from a data source, the matching in= variable gets set to 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When considering the first question, the biggest issue is that SAS reads each observation once. &amp;nbsp;Even in a many-to-one MERGE where one observation matches many observations from the "many" data set, SAS still reads each observation once. &amp;nbsp;The values read in may be retained (so that the "one" variables repeat for each of the "many" observations). &amp;nbsp;But each observation gets read in once.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So what happens in the sample program? &amp;nbsp;The program sets the in= variables to 0. &amp;nbsp;The program resets them to 1 only when it actually reads in an observation. &amp;nbsp;When one source has 5 observations and the other source has 3 observations ... the 5 observation data set has an observation read from it each time, and its in= variable gets set to 1 each time. &amp;nbsp;The 3 observation data set has an observation read in just 3 times, and so has its in= variable set to 1 just 3 times. &amp;nbsp;For the remaining 2 nonmatching observations, hard-coding the in= variables to 0 means that the 0 value remains in place.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The best way to view this might be to add 3 PUT statements:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;put &amp;nbsp;from_d1= from_d2=;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Add this statement at 3 points in the DATA step: &amp;nbsp;(1) just after the DATA statement, (2) just before the MERGE statement, and (3) just after the BY statement. &amp;nbsp;That might help illustrate how the software sets, resets, and utilizes those variables.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Aug 2016 19:27:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simple-Data-Merge-question/m-p/289871#M59899</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-08-05T19:27:02Z</dc:date>
    </item>
    <item>
      <title>Re: Simple Data Merge question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simple-Data-Merge-question/m-p/289874#M59901</link>
      <description>&lt;P&gt;You are simply awesome. Thanks for explaining that to me in detail.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Aug 2016 19:48:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simple-Data-Merge-question/m-p/289874#M59901</guid>
      <dc:creator>deepanshub</dc:creator>
      <dc:date>2016-08-05T19:48:53Z</dc:date>
    </item>
    <item>
      <title>Re: Simple Data Merge question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simple-Data-Merge-question/m-p/404210#M98252</link>
      <description>&lt;P&gt;when do we use merge ?? any clear notes or video file...Tx...&lt;/P&gt;</description>
      <pubDate>Sat, 14 Oct 2017 11:28:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simple-Data-Merge-question/m-p/404210#M98252</guid>
      <dc:creator>Vittal17</dc:creator>
      <dc:date>2017-10-14T11:28:45Z</dc:date>
    </item>
  </channel>
</rss>

