<?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: merging data sets using set statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/merging-data-sets-using-set-statement/m-p/967053#M376272</link>
    <description>&lt;P&gt;Your MERGE statement is missing the semicolon.&lt;/P&gt;
&lt;P&gt;You do have any BY statement .&amp;nbsp; How is the data step supposed to know which observations from A should be combined with which observations from B?&lt;/P&gt;
&lt;P&gt;Both datasets have the exact same variables.&amp;nbsp; So merging them in that way will overwrite the values from A with the values from B until B runs out of observations.&amp;nbsp; And if you don't want that extra observation from A then why bother using it at all?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 20 May 2025 20:49:14 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2025-05-20T20:49:14Z</dc:date>
    <item>
      <title>merging data sets using set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-data-sets-using-set-statement/m-p/967046#M376268</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I would like to merge 2 dataset. data A has a value for my region= RRE, AAA, OTHER etc and data B&amp;nbsp; has region = RRE, AAA etc.&amp;nbsp; Data B does NOT have the OTHER but data A has both OTHER and the correct value.&amp;nbsp;&lt;/P&gt;&lt;P&gt;After merging,&amp;nbsp;I want my final data to have only the correct value from data B instead of the OTHER from data A.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data A;
input Region $ 1-5 Installation $;
datalines;
RRE  	USS
OTHER	USS
OTHER	USS
AAA   	USTU
AAA     USTU
OTHER	USS
OTHER   USTU
OTHER	UNSP
OTHER	UNSP
OTHER	UNSP
; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data B ;
input Region $ 1-5 Installation $;
datalines;
RRE     USS
RRE     USS
RRE     USS
AAA     USTU
AAA     USTU
AAA     USTU
OTHER UNSP
OTHER UNSP
OTHER UNSP
; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;output&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;data C&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;RRE&amp;nbsp;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;USS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;RRE&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;USS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;RRE&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;USS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;RRE&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;USS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAA&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;USTU&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAA&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;USTU&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAA&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;USTU&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;OTHER&lt;/TD&gt;&lt;TD&gt;UNSP&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;OTHER&lt;/TD&gt;&lt;TD&gt;UNSP&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;OTHER&lt;/TD&gt;&lt;TD&gt;UNSP&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;My code is not giving the expected output. I also used the line which I commented out but still did not get the expected output C&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data C; 
    merge A (in=in1 ) 
          B (in=in2)
if in2 then REGION = in1;
*if REGION = 'OTHER' and installation ne 'UNSP' then REGION = 'in2';
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 May 2025 20:34:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-data-sets-using-set-statement/m-p/967046#M376268</guid>
      <dc:creator>CathyVI</dc:creator>
      <dc:date>2025-05-20T20:34:33Z</dc:date>
    </item>
    <item>
      <title>Re: merging data sets using set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-data-sets-using-set-statement/m-p/967048#M376269</link>
      <description>&lt;P&gt;In the example you have provided,&amp;nbsp; you don't need the data set A at all.&amp;nbsp; You coudl simply forget about merging and just use:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data C;
set B;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please include an example where the data set A is needed.&lt;/P&gt;</description>
      <pubDate>Tue, 20 May 2025 19:43:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-data-sets-using-set-statement/m-p/967048#M376269</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2025-05-20T19:43:57Z</dc:date>
    </item>
    <item>
      <title>Re: merging data sets using set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-data-sets-using-set-statement/m-p/967050#M376270</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please confirm that you expect data set A to be the same as what the following code creates:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
  input
    Region       : $char5.
    Installation   $char50.
  ;
  
  datalines;
RRE USS OTHER USS OTHER USS
AAA USTU
AAA USTU
OTHER USTU OTHER UNSP OTHER UNSP OTHER UNSP
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If data set A should be different then please clarify by editing the above code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Lastly, please clarify what you expect the output data set to look like by sharing code like the above, but for your output based on the input data sets A &amp;amp; B that you have posted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks &amp;amp; kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: Clarification.&lt;/P&gt;</description>
      <pubDate>Tue, 20 May 2025 20:05:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-data-sets-using-set-statement/m-p/967050#M376270</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2025-05-20T20:05:52Z</dc:date>
    </item>
    <item>
      <title>Re: merging data sets using set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-data-sets-using-set-statement/m-p/967051#M376271</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/22588"&gt;@Amir&lt;/a&gt;&amp;nbsp; Sorry for the mix-up. I have updated the message. In the output, i want the number of records to be 10 records not 9.&lt;/P&gt;</description>
      <pubDate>Tue, 20 May 2025 20:36:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-data-sets-using-set-statement/m-p/967051#M376271</guid>
      <dc:creator>CathyVI</dc:creator>
      <dc:date>2025-05-20T20:36:37Z</dc:date>
    </item>
    <item>
      <title>Re: merging data sets using set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-data-sets-using-set-statement/m-p/967053#M376272</link>
      <description>&lt;P&gt;Your MERGE statement is missing the semicolon.&lt;/P&gt;
&lt;P&gt;You do have any BY statement .&amp;nbsp; How is the data step supposed to know which observations from A should be combined with which observations from B?&lt;/P&gt;
&lt;P&gt;Both datasets have the exact same variables.&amp;nbsp; So merging them in that way will overwrite the values from A with the values from B until B runs out of observations.&amp;nbsp; And if you don't want that extra observation from A then why bother using it at all?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 May 2025 20:49:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-data-sets-using-set-statement/m-p/967053#M376272</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-05-20T20:49:14Z</dc:date>
    </item>
    <item>
      <title>Re: merging data sets using set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-data-sets-using-set-statement/m-p/967061#M376273</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for the updates. Please respond explaining what the logical rules are for bringing the two data sets together.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, RRE is in data set A once and 3 times in data set B then 4 times in the output; is this 1 from A and 3 from B?&lt;/P&gt;
&lt;P&gt;Also, AAA appears twice in data set A and three times in data set B then 3 times in the output; is this only from B?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Lastly, your question says "&lt;SPAN&gt;Data B does NOT have the OTHER ...&lt;/SPAN&gt;", but there are 3 OTHER records in B. Please clarify.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks &amp;amp; kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Tue, 20 May 2025 21:45:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-data-sets-using-set-statement/m-p/967061#M376273</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2025-05-20T21:45:16Z</dc:date>
    </item>
    <item>
      <title>Re: merging data sets using set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-data-sets-using-set-statement/m-p/967082#M376282</link>
      <description>&lt;P&gt;Below two possible options.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* option 1 */
data want_1;
  if _n_=1 then
    do;
      dcl hash h1(dataset:'a');
      h1.defineKey('Installation');
      h1.defineData('Region');
      h1.defineDone();
    end;
  set b;
  _rc=h1.find();
  drop _rc;
run;
proc print data=want_1;
run;

/* option 2 */
proc sort nodupkey data=a out=a_unique;
  by Installation;
run;
proc sort data=b;
  by Installation;
run;
data want_2;
  merge b a_unique;
  by Installation;
run;
proc print data=want_2;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 May 2025 08:40:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-data-sets-using-set-statement/m-p/967082#M376282</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2025-05-21T08:40:38Z</dc:date>
    </item>
  </channel>
</rss>

