<?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 two datasets by ID and date in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Merging-two-datasets-by-ID-and-date/m-p/318408#M9153</link>
    <description>&lt;P&gt;I am expecting to get this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID Date x1 x2 x3 y1&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; 0 &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; 2 &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; 0 &amp;nbsp; 1 &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; 3 &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; 1 &amp;nbsp; 1 &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; 4 &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; 1 &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; 5 &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; 1 &amp;nbsp; 1 &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There should only be one record per date, but the data step results in there being an extra record for the dates that are also in B.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have also tried in SQL, but have gotten similar results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table&amp;nbsp;AB as&lt;BR /&gt;select *&lt;BR /&gt;from A&amp;nbsp;as a left join B&amp;nbsp;as b&lt;BR /&gt;on a.ID=b.cID and a.date=b.date;&lt;BR /&gt;quit;&lt;/P&gt;</description>
    <pubDate>Mon, 12 Dec 2016 22:56:10 GMT</pubDate>
    <dc:creator>jwestra84</dc:creator>
    <dc:date>2016-12-12T22:56:10Z</dc:date>
    <item>
      <title>Merging two datasets by ID and date</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Merging-two-datasets-by-ID-and-date/m-p/318404#M9151</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am having a little trouble merging two datasets together.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dataset A:&lt;/P&gt;
&lt;P&gt;ID Date x1 x2 x3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dataset B:&lt;/P&gt;
&lt;P&gt;ID Date y1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Both datasets are one record per date, but A will have 5 dates per ID, whereas B will have anywhere from 0 to 6 dates per ID. Not every ID in A will have records in B.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I first tried this, it was merging correctly for the first occurance per id, but afterwards would just add the records from&amp;nbsp;B as if I were interleaving the datasets. I used the following code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data AB; merge A B;&lt;/P&gt;
&lt;P&gt;by ID date;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The resulting dataset looks&amp;nbsp;like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID Date x1 x2 x3 y1&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; 0 &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; 2 &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; 0 &amp;nbsp; 1 &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; 3 &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; 1 &amp;nbsp; 1 &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; 4 &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; 1 &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; 5 &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; 1 &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; 5 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So set A has all five dates and set B has records at dates 2 and 5. I would like it so that the two records with ID 1 and date 5 were the same record.&lt;/P&gt;
&lt;P&gt;Any help would be appreciated!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 12 Dec 2016 22:36:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Merging-two-datasets-by-ID-and-date/m-p/318404#M9151</guid>
      <dc:creator>jwestra84</dc:creator>
      <dc:date>2016-12-12T22:36:00Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two datasets by ID and date</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Merging-two-datasets-by-ID-and-date/m-p/318406#M9152</link>
      <description>&lt;P&gt;Many to many merges aren't what you expect in a data step. Using SQL in this case is recommended.&lt;/P&gt;
&lt;P&gt;You show what you're getting, but not what you expect ...what do you expect as results?&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/kb/24/752.html" target="_blank"&gt;http://support.sas.com/kb/24/752.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Dec 2016 22:47:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Merging-two-datasets-by-ID-and-date/m-p/318406#M9152</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-12-12T22:47:00Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two datasets by ID and date</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Merging-two-datasets-by-ID-and-date/m-p/318408#M9153</link>
      <description>&lt;P&gt;I am expecting to get this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID Date x1 x2 x3 y1&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; 0 &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; 2 &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; 0 &amp;nbsp; 1 &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; 3 &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; 1 &amp;nbsp; 1 &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; 4 &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; 1 &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; 5 &amp;nbsp; &amp;nbsp; &amp;nbsp;0 &amp;nbsp; 1 &amp;nbsp; 1 &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There should only be one record per date, but the data step results in there being an extra record for the dates that are also in B.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have also tried in SQL, but have gotten similar results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table&amp;nbsp;AB as&lt;BR /&gt;select *&lt;BR /&gt;from A&amp;nbsp;as a left join B&amp;nbsp;as b&lt;BR /&gt;on a.ID=b.cID and a.date=b.date;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Dec 2016 22:56:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Merging-two-datasets-by-ID-and-date/m-p/318408#M9153</guid>
      <dc:creator>jwestra84</dc:creator>
      <dc:date>2016-12-12T22:56:10Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two datasets by ID and date</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Merging-two-datasets-by-ID-and-date/m-p/318437#M9156</link>
      <description>&lt;P&gt;You will need to check your data. &amp;nbsp;The results you describe do not match the program you showed. &amp;nbsp;The program would perform exactly as you hoped and expected. &amp;nbsp;These results correspond to data set B having 2 observations for DATE=5. &amp;nbsp;On the first of those, Y1 is missing and on the second, Y1 is 2.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Dec 2016 03:22:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Merging-two-datasets-by-ID-and-date/m-p/318437#M9156</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-12-13T03:22:58Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two datasets by ID and date</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Merging-two-datasets-by-ID-and-date/m-p/318439#M9157</link>
      <description>&lt;P&gt;I suspect that your dates don't really match all the time. Try rounding the dates before joining :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table AB as
select a.*, b.y
from A as a left join B as b
on a.ID=b.ID and round(a.date)=round(b.date);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Dec 2016 03:28:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Merging-two-datasets-by-ID-and-date/m-p/318439#M9157</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-12-13T03:28:34Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two datasets by ID and date</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Merging-two-datasets-by-ID-and-date/m-p/318441#M9158</link>
      <description>&lt;P&gt;Thank you for calling my attention to this. Turns out this was the problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 13 Dec 2016 03:35:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Merging-two-datasets-by-ID-and-date/m-p/318441#M9158</guid>
      <dc:creator>jwestra84</dc:creator>
      <dc:date>2016-12-13T03:35:37Z</dc:date>
    </item>
    <item>
      <title>Re: Merging two datasets by ID and date</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Merging-two-datasets-by-ID-and-date/m-p/318618#M9162</link>
      <description>&lt;P&gt;I'm glad you were able to identify the problem, &lt;A id="link_8" class="lia-link-navigation lia-page-link lia-user-name-link" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/9953" target="_self"&gt;&lt;SPAN class=""&gt;jwestra84&lt;/SPAN&gt;&lt;/A&gt;! If one of the replies was the exact solution to your problem, can you "Accept it as a solution"? Or if one was particularly helpful, feel free to "Like" it. This will help other community members who may run into the same issue know what worked.&lt;BR /&gt;&lt;BR /&gt;Thanks!&lt;BR /&gt;Anna&lt;/P&gt;</description>
      <pubDate>Tue, 13 Dec 2016 16:26:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Merging-two-datasets-by-ID-and-date/m-p/318618#M9162</guid>
      <dc:creator>AnnaBrown</dc:creator>
      <dc:date>2016-12-13T16:26:47Z</dc:date>
    </item>
  </channel>
</rss>

