<?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: How do I merge two dataset and keep the data with smallest date difference? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/922415#M41396</link>
    <description>Thank you so much!</description>
    <pubDate>Mon, 01 Apr 2024 03:57:06 GMT</pubDate>
    <dc:creator>tan-wongv</dc:creator>
    <dc:date>2024-04-01T03:57:06Z</dc:date>
    <item>
      <title>How do I merge two dataset and keep the data with smallest date difference?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/921055#M41305</link>
      <description>&lt;P&gt;I have two data set that I'd like to merge and keep only one recorded time that is the same or closest to the diagnosis date. (The date different should not exceed 365 days)&lt;/P&gt;&lt;P&gt;Dataset1:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;DMRN&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;dx_date&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;age&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;284&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;07SEP2017&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&amp;nbsp;65&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;285&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;27APR2021&amp;nbsp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t;&lt;TD&gt;age&lt;/TD&gt;&lt;TD&gt;recorded_time&lt;/TD&gt;&lt;TD&gt;dressing&lt;/TD&gt;&lt;TD&gt;grooming&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;284&lt;/TD&gt;&lt;TD&gt;07SEP2017&lt;/TD&gt;&lt;TD&gt;65&lt;/TD&gt;&lt;TD&gt;07SEP2017&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;285&lt;/TD&gt;&lt;TD&gt;27APR2021&lt;/TD&gt;&lt;TD&gt;78&lt;/TD&gt;&lt;TD&gt;02APR2021&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;286&lt;/TD&gt;&lt;TD&gt;03MAY2019&lt;/TD&gt;&lt;TD&gt;72&lt;/TD&gt;&lt;TD&gt;03MAY2019&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to merge both dataset by DMRN and calculate date difference between diag date and recorded time, but I don't know which code to use to keep only one recorded time that is the same or closest to diag date(not exceed 365 days). Any help or suggestion would be very appreciated. Thank you.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Mar 2024 05:08:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/921055#M41305</guid>
      <dc:creator>tan-wongv</dc:creator>
      <dc:date>2024-03-20T05:08:00Z</dc:date>
    </item>
    <item>
      <title>How do I merge two dataset and keep only the data with smallest date differences?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/921054#M41331</link>
      <description>&lt;P&gt;I have two data set that would like to merge and keep only one recorded time that is the same or closest to the diagnosis date. (The date different cannot exceed 365 days)&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Dataset 1:&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;DMRN&amp;nbsp; &amp;nbsp; DX_DATE&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AGE&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;284&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;07SEP2017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 65&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;285&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 27APR2021&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 78&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;286&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 03MAY2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 72&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Dataset 2:&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;DMRN&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;recorded_time&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Dressing&amp;nbsp; &amp;nbsp; &amp;nbsp; Feeding&lt;/P&gt;&lt;P&gt;284&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 07SEP2017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&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;284&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;14JUN2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;284&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;26SEP2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;285&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;02APR2021&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&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;285&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 22OCT2021&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&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;286&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 03MAY2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;286&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 27NOV2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;I would like the dataset to look like:&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;DMRN&amp;nbsp; &amp;nbsp; DX_DATE&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AGE&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; recorded_time&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Dressing&amp;nbsp; &amp;nbsp; &amp;nbsp; Feeding&lt;/P&gt;&lt;P&gt;284&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;07SEP2017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 65&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 07SEP2017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&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;285&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 27APR2021&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 78&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;02APR2021&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&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;286&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 03MAY2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 72&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;03MAY2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to merge both dataset by DMRN and calculate date difference between dx_date and recorded_time, but I don't know which code to use to keep only one recorde_time that is the same or closest to DX_DATE (not exceed 365 days). Any help or suggestion would be very appreciate. Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 20 Mar 2024 04:53:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/921054#M41331</guid>
      <dc:creator>tan-wongv</dc:creator>
      <dc:date>2024-03-20T04:53:47Z</dc:date>
    </item>
    <item>
      <title>Re: How do I merge two dataset and keep the data with smallest date difference?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/921064#M41306</link>
      <description>&lt;P&gt;As a courtesy, please provide your sampla data in a data step form next time.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table want as 
		select have1.*, have2.recorded_time, have2.dressing, have2.grooming
		from have1, have2
		where have1.dmrn = have2.dmrn
		group by have1.dmrn
		having min(abs(dx_date - recorded_time)) = abs(dx_date - recorded_time)
	;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Mar 2024 08:01:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/921064#M41306</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2024-03-20T08:01:00Z</dc:date>
    </item>
    <item>
      <title>Re: How do I merge two dataset and keep the data with smallest date difference?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/921065#M41307</link>
      <description>&lt;P&gt;Something like below should work&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
  input DMRN dx_date:date9. age;
  format dx_date date9.;
  datalines;
284 07SEP2017 65
285 27APR2021 78
286 03MAY2019 72
;

data have2;
  input DMRN recorded_time:date11. dressing grooming;
  format recorded_time date9.;
  datalines;
284 7-Sep-17 1 2
284 14JUN2018 1 1
284 26SEP2018 1 1
285 2-Apr-21 2 2
285 22-Oct-21 2 2
286 3-May-19 1 1
286 27-Nov-19 1 1
;

proc sql;
/*  create table want as*/
  select l.*, r.recorded_time, r.dressing, r.grooming
  from have1 l inner join have2 r
    on l.dmrn=r.dmrn
  group by l.DMRN
  having abs(r.recorded_time-l.dx_date) = min(abs(r.recorded_time-l.dx_date))
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Mar 2024 08:14:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/921065#M41307</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-03-20T08:14:00Z</dc:date>
    </item>
    <item>
      <title>Re: How do I merge two dataset and keep only the data with smallest date differences?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/921158#M41332</link>
      <description>&lt;P&gt;First I'm going to create the datasets and sort dataset1 by DX_DATE and dataset2 by recorded_time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data dataset1;
    input DMRN DX_DATE : date9. AGE;
    format DX_DATE date9.;
    datalines;
284 07SEP2017 65
285 27APR2021 78
286 03MAY2019 72;
run;

data dataset2;
    input DMRN recorded_time : date9. Dressing Feeding;
    format recorded_time date9.;
    datalines;
284 07SEP2017 1 2
284 14JUN2018 1 1
284 26SEP2018 1 1
285 02APR2021 2 2
285 22OCT2021 2 2
286 03MAY2019 1 1
286 27NOV2019 1 1;
run;&lt;BR /&gt;&lt;BR /&gt;proc sort data=dataset1; by DMRN DX_DATE; run;&lt;BR /&gt;proc sort data=dataset2; by DMRN recorded_time; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I like to work in small sections to see if everything works as it should so I'm merging the datasets and creating a new variable called diff. The variable diff is the number of days between DX_DATE and recorded_time. Then I'm going to sort the new dataset by DMRN and diff, so the first row of every DMRN is the one with the smallest date difference.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data difference;
    merge dataset1 dataset2;
    by DMRN;
    diff= abs(DX_DATE-recorded_time);
    keep DMRN DX_DATE AGE recorded_time Dressing Feeding diff;
run;

proc sort data=difference;
    by DMRN diff;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now that everyting is sorted, I'm going to create the dataset you asked for by retaining only the first row of every DMRN (I also dropped the column diff, I assumed you'd have no need for it).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want (drop=diff);
    set difference;
    by DMRN;
    if first.DMRN;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope this helps!&lt;/P&gt;</description>
      <pubDate>Wed, 20 Mar 2024 18:20:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/921158#M41332</guid>
      <dc:creator>veronicasalmaso</dc:creator>
      <dc:date>2024-03-20T18:20:52Z</dc:date>
    </item>
    <item>
      <title>Re: How do I merge two dataset and keep only the data with smallest date differences?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/921250#M41333</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/464169"&gt;@tan-wongv&lt;/a&gt;&amp;nbsp;Please ask the same question once only. It looks to me that you've asked the same question already &lt;A href="https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/921055" target="_self"&gt;here&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Mar 2024 05:56:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/921250#M41333</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-03-21T05:56:08Z</dc:date>
    </item>
    <item>
      <title>Re: How do I merge two dataset and keep only the data with smallest date differences?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/921638#M41347</link>
      <description>&lt;P&gt;My first post was detected as a spam, so I tried to post a new discussion. I don't know why now my first post also appears on the discussion board. I will make sure to double check my post next time. Thank you for pointing this out &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Mar 2024 20:22:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/921638#M41347</guid>
      <dc:creator>tan-wongv</dc:creator>
      <dc:date>2024-03-24T20:22:15Z</dc:date>
    </item>
    <item>
      <title>Re: How do I merge two dataset and keep only the data with smallest date differences?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/921639#M41348</link>
      <description>&lt;P&gt;Thank you for your response! I have actually tried this before. After this method, I would like to keep only the value of ADL that has number of days between recorded_time and Dx_date less than 365 days, but I don't know which code should I use.&lt;/P&gt;</description>
      <pubDate>Sun, 24 Mar 2024 20:28:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/921639#M41348</guid>
      <dc:creator>tan-wongv</dc:creator>
      <dc:date>2024-03-24T20:28:38Z</dc:date>
    </item>
    <item>
      <title>Re: How do I merge two dataset and keep the data with smallest date difference?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/921640#M41349</link>
      <description>&lt;P&gt;Thank you very much for your response! How about if I want to keep the data with smallest date difference, but the difference should not be more than 365 days? Can you please also help me with this issue?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 Mar 2024 20:31:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/921640#M41349</guid>
      <dc:creator>tan-wongv</dc:creator>
      <dc:date>2024-03-24T20:31:14Z</dc:date>
    </item>
    <item>
      <title>Re: How do I merge two dataset and keep the data with smallest date difference?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/921649#M41350</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/464169"&gt;@tan-wongv&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you very much for your response! How about if I want to keep the data with smallest date difference, but the difference should not be more than 365 days? Can you please also help me with this issue?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Just add this condition to the filter expression.&lt;/P&gt;
&lt;PRE&gt;having abs(r.recorded_time-l.dx_date)&amp;lt;=365 and abs(r.recorded_time-l.dx_date) = min(abs(r.recorded_time-l.dx_date))&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Or even better already to the join condition (if using inner join) so the rows get already dropped prior to group by processing&lt;/P&gt;
&lt;PRE&gt;on l.dmrn=r.dmrn and abs(r.recorded_time-l.dx_date)&amp;lt;=365&lt;/PRE&gt;
&lt;P&gt;Or via a where clause works as well&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql _method _tree;
/*  create table want as*/
  select l.*, r.recorded_time, r.dressing, r.grooming
  from have1 l inner join have2 r
    on l.dmrn=r.dmrn
  where abs(r.recorded_time-l.dx_date)&amp;lt;=365
  group by l.DMRN
  having abs(r.recorded_time-l.dx_date) = min(abs(r.recorded_time-l.dx_date))
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Mar 2024 01:24:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/921649#M41350</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-03-25T01:24:16Z</dc:date>
    </item>
    <item>
      <title>Re: How do I merge two dataset and keep the data with smallest date difference?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/922415#M41396</link>
      <description>Thank you so much!</description>
      <pubDate>Mon, 01 Apr 2024 03:57:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-merge-two-dataset-and-keep-the-data-with-smallest-date/m-p/922415#M41396</guid>
      <dc:creator>tan-wongv</dc:creator>
      <dc:date>2024-04-01T03:57:06Z</dc:date>
    </item>
  </channel>
</rss>

