<?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 two data sets by conditionally in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/merge-two-data-sets-by-conditionally/m-p/412603#M279871</link>
    <description>&lt;P&gt;Your conclusion is right:&lt;/P&gt;
&lt;PRE&gt; merge in a few steps, like merge non missing id2  first and merge missing id2 at the second step.&lt;/PRE&gt;</description>
    <pubDate>Sat, 11 Nov 2017 20:09:50 GMT</pubDate>
    <dc:creator>Shmuel</dc:creator>
    <dc:date>2017-11-11T20:09:50Z</dc:date>
    <item>
      <title>merge two data sets by conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merge-two-data-sets-by-conditionally/m-p/412595#M279870</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I would like to merge the following two data sets (data set A and B) where each row is uniquely identified by id1 and id2 both in each data set (no duplicates). The merge is done by variables id1 and id2 but with some additional condition:&lt;/P&gt;
&lt;P&gt;if id2 is not missing in data set B, then merge by two varaibles id1 and id2 both;&lt;/P&gt;
&lt;P&gt;if id2 is missing in data set B, then merge by id1 only (i.e., when there are multiple rows in data set A, one row in B is merged to multiple rows in A, such as id1=4)&lt;/P&gt;
&lt;P&gt;The issue is the missing values for id2 in data set B.&lt;/P&gt;
&lt;P&gt;I wondered if you have any clean way to do this merge? What I can come up is to do the merge in a few steps, like merge non missing id2 &amp;nbsp;first and merge missing id2 at the second step. Thank you for your time!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Mandy1&lt;/P&gt;
&lt;P&gt;*************Data set A************&lt;/P&gt;
&lt;P&gt;id1 id2 &amp;nbsp;wage&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp;101 &amp;nbsp; &amp;nbsp;1000&lt;/P&gt;
&lt;P&gt;2 &amp;nbsp;302 &amp;nbsp; &amp;nbsp;2000&lt;/P&gt;
&lt;P&gt;2 &amp;nbsp;305 &amp;nbsp; &amp;nbsp;3000&lt;/P&gt;
&lt;P&gt;3 &amp;nbsp;400 &amp;nbsp; &amp;nbsp;5000&lt;/P&gt;
&lt;P&gt;4 &amp;nbsp;500 &amp;nbsp; &amp;nbsp;5000&lt;/P&gt;
&lt;P&gt;4 &amp;nbsp;502 &amp;nbsp; &amp;nbsp;6000&lt;/P&gt;
&lt;P&gt;*************Data set B************&lt;/P&gt;
&lt;P&gt;id1 id2 &amp;nbsp;hours&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; &amp;nbsp; . &amp;nbsp; &amp;nbsp; &amp;nbsp;40&lt;/P&gt;
&lt;P&gt;2 &amp;nbsp;302 &amp;nbsp; &amp;nbsp;20&lt;/P&gt;
&lt;P&gt;2 &amp;nbsp;305 &amp;nbsp; &amp;nbsp;40&lt;/P&gt;
&lt;P&gt;3 &amp;nbsp;400 &amp;nbsp; &amp;nbsp;35&lt;/P&gt;
&lt;P&gt;4 &amp;nbsp; . &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;40&lt;/P&gt;
&lt;P&gt;*************desired resulted data set************&lt;/P&gt;
&lt;P&gt;id1 id2 &amp;nbsp;wage hours&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp;101 &amp;nbsp; &amp;nbsp;1000 &amp;nbsp;40&lt;/P&gt;
&lt;P&gt;2 &amp;nbsp;302 &amp;nbsp; &amp;nbsp;2000 &amp;nbsp; 20&lt;/P&gt;
&lt;P&gt;2 &amp;nbsp;305 &amp;nbsp; &amp;nbsp;3000 &amp;nbsp; 40&lt;/P&gt;
&lt;P&gt;3 &amp;nbsp;400 &amp;nbsp; &amp;nbsp;5000 &amp;nbsp; 35&lt;/P&gt;
&lt;P&gt;4 &amp;nbsp;500 &amp;nbsp; &amp;nbsp;5000 &amp;nbsp; 40&lt;/P&gt;
&lt;P&gt;4 &amp;nbsp;502 &amp;nbsp; &amp;nbsp;6000 &amp;nbsp; 40&lt;/P&gt;</description>
      <pubDate>Sat, 11 Nov 2017 19:21:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merge-two-data-sets-by-conditionally/m-p/412595#M279870</guid>
      <dc:creator>Mandy1</dc:creator>
      <dc:date>2017-11-11T19:21:42Z</dc:date>
    </item>
    <item>
      <title>Re: merge two data sets by conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merge-two-data-sets-by-conditionally/m-p/412603#M279871</link>
      <description>&lt;P&gt;Your conclusion is right:&lt;/P&gt;
&lt;PRE&gt; merge in a few steps, like merge non missing id2  first and merge missing id2 at the second step.&lt;/PRE&gt;</description>
      <pubDate>Sat, 11 Nov 2017 20:09:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merge-two-data-sets-by-conditionally/m-p/412603#M279871</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-11-11T20:09:50Z</dc:date>
    </item>
    <item>
      <title>Re: merge two data sets by conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merge-two-data-sets-by-conditionally/m-p/412628#M279872</link>
      <description>&lt;P&gt;You could do two data step, each merging A against a subset of B.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Editted note:&amp;nbsp; Actually, given the assumptions noted below, you can do a single step merge, by ignoring ID2 in data set B:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Sasfont"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; want;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;&amp;nbsp; merge&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; a b (drop=id2);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;&amp;nbsp; by&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; id1;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Sasfont"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But, with two SET statements, it is possible to do this in a single data step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
  input id1 id2  wage;
datalines;
1  101    1000
2  302    2000
2  305    3000
3  400    5000
4  500    5000
4  502    6000
run;
data b;
  input id1 id2  hours;
datalines;
1     .      40
2  302    20
2  305    40
3  400    35
4   .        40
run;


data want (drop=_:);
  set b (rename=(id2=_id2b));
  do until (id2=_id2b or last.id1=1);
    set a;
    by id1;
    output;
  end;
run;
	
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;Note:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;I assume that both data sets are sorted by ID1/ID2.&lt;/LI&gt;
&lt;LI&gt;For each ID1, data set B has either the same sequence of ID2 value as A, or it has a single observations with ID2=.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Sun, 12 Nov 2017 00:02:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merge-two-data-sets-by-conditionally/m-p/412628#M279872</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-11-12T00:02:08Z</dc:date>
    </item>
    <item>
      <title>Re: merge two data sets by conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merge-two-data-sets-by-conditionally/m-p/412638#M279873</link>
      <description>&lt;P&gt;Can also be done with SQL:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table C as
select 
    a.id1,
    a.id2,
    wage,
    hours
from a inner join b on 
    a.id1=b.id1 and (a.id2=b.id2 or b.id2 is missing);
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 12 Nov 2017 03:46:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merge-two-data-sets-by-conditionally/m-p/412638#M279873</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-11-12T03:46:19Z</dc:date>
    </item>
    <item>
      <title>Re: merge two data sets by conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merge-two-data-sets-by-conditionally/m-p/412693#M279874</link>
      <description>&lt;P&gt;Thank you all for the above responses!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the SQL way, based on my experience in the past I encountered the following issue:&lt;/P&gt;
&lt;P&gt;When the "join on " statement in SQL contains a "or" compared to all using "and," the "join" process takes much much longer to complete. Once I remember clearly that it seemed like it will take forever; then I gave up and broke that "join" into two "join" so there is no "or" in the "join" process. That is much quicker. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Has anyone noticed the same issue? Why join on containing an "or" takes longer?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Sun, 12 Nov 2017 15:38:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merge-two-data-sets-by-conditionally/m-p/412693#M279874</guid>
      <dc:creator>Mandy1</dc:creator>
      <dc:date>2017-11-12T15:38:02Z</dc:date>
    </item>
    <item>
      <title>Re: merge two data sets by conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merge-two-data-sets-by-conditionally/m-p/412753#M279875</link>
      <description>&lt;P&gt;True. Other users have mentioned it as well.&amp;nbsp;I always hesitate before using an OR condition in SAS/SQL. It seems to be&amp;nbsp;a weakness of SAS/SQL. But then, I never wrote an SQL interpreter myself &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Nov 2017 23:39:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merge-two-data-sets-by-conditionally/m-p/412753#M279875</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-11-12T23:39:42Z</dc:date>
    </item>
    <item>
      <title>Re: merge two data sets by conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merge-two-data-sets-by-conditionally/m-p/413981#M279876</link>
      <description>&lt;P&gt;Thank you all for your help with this question!&lt;/P&gt;</description>
      <pubDate>Thu, 16 Nov 2017 13:12:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merge-two-data-sets-by-conditionally/m-p/413981#M279876</guid>
      <dc:creator>Mandy1</dc:creator>
      <dc:date>2017-11-16T13:12:55Z</dc:date>
    </item>
  </channel>
</rss>

