<?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 Conditionally Merging 2 Datasets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-Merging-2-Datasets/m-p/544010#M150407</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've got data that looks like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id key value;
datalines;
1 534 2905
2 . 6304
3 872 1108
;
run;

data two;
input id key;
datalines;
. 534
2 298
3 872
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like for it to be merged by both id and key such that I get the following results:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data end;
input id key value;
datalines;
1 534 2905
2 298 6304
3 872 1108
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;At the moment I've only be merging by id am not sure of if the merge function can be used conditionally on if id is missing, then merge on key. I'm also not extremely familiar with proc sql, but am willing to use whatever works for the code. Many thanks!&lt;/P&gt;</description>
    <pubDate>Mon, 18 Mar 2019 15:21:59 GMT</pubDate>
    <dc:creator>jdchang</dc:creator>
    <dc:date>2019-03-18T15:21:59Z</dc:date>
    <item>
      <title>Conditionally Merging 2 Datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-Merging-2-Datasets/m-p/544010#M150407</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've got data that looks like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id key value;
datalines;
1 534 2905
2 . 6304
3 872 1108
;
run;

data two;
input id key;
datalines;
. 534
2 298
3 872
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like for it to be merged by both id and key such that I get the following results:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data end;
input id key value;
datalines;
1 534 2905
2 298 6304
3 872 1108
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;At the moment I've only be merging by id am not sure of if the merge function can be used conditionally on if id is missing, then merge on key. I'm also not extremely familiar with proc sql, but am willing to use whatever works for the code. Many thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 18 Mar 2019 15:21:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-Merging-2-Datasets/m-p/544010#M150407</guid>
      <dc:creator>jdchang</dc:creator>
      <dc:date>2019-03-18T15:21:59Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Merging 2 Datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-Merging-2-Datasets/m-p/544021#M150411</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/243966"&gt;@jdchang&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;&amp;nbsp; &amp;nbsp;You could join the data set by using the common variables like so:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data merged;
   set one two;
   by key;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; Once you have a merged data set you can filter the data like so:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data want;

     set merged;

where key ne . ;&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Mar 2019 15:42:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-Merging-2-Datasets/m-p/544021#M150411</guid>
      <dc:creator>UdayGuntupalli</dc:creator>
      <dc:date>2019-03-18T15:42:48Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Merging 2 Datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-Merging-2-Datasets/m-p/544023#M150412</link>
      <description>&lt;P&gt;How do you know to treat the first observation of the second dataset as if the value of ID as 1?&lt;/P&gt;</description>
      <pubDate>Mon, 18 Mar 2019 15:44:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-Merging-2-Datasets/m-p/544023#M150412</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-03-18T15:44:59Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Merging 2 Datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-Merging-2-Datasets/m-p/544024#M150413</link>
      <description>&lt;P&gt;You might need to post a more complete example of the issues you have.&lt;/P&gt;
&lt;P&gt;Your current example is just a merge by ID and collapse values of KEY and ignore values from second dataset that don't match.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge one (in=in1) two (rename=(key=key2));
  by id;
  if in1;
  key=coalesce(key,key2);
  drop key2;
run;
 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Mar 2019 15:48:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-Merging-2-Datasets/m-p/544024#M150413</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-03-18T15:48:31Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Merging 2 Datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-Merging-2-Datasets/m-p/544026#M150414</link>
      <description>&lt;P&gt;You probably need to provide more information.&amp;nbsp; Are these data sets really representative of the problem?&amp;nbsp; Or do they contain more variables than you are showing?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If a variable has a missing value in TWO, you might as well throw out the entire observation.&amp;nbsp; Even if you could match on the other variable, an observation with a missing value cannot contribute any new information.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could there be more than one observation for a KEY?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could there be more than one observation for an ID?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do the data sets always match up the way you pictured:&amp;nbsp; first observation with first observation, second observation with second observation, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are ways to handle most of the questions, but more direction is needed.&amp;nbsp; Otherwise, we are just guessing.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Mar 2019 16:08:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-Merging-2-Datasets/m-p/544026#M150414</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-03-18T16:08:57Z</dc:date>
    </item>
  </channel>
</rss>

