<?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 merge two datasets by nearest date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/merge-two-datasets-by-nearest-date/m-p/882319#M348603</link>
    <description>&lt;P&gt;Hi, I need to merge two datasets by a unique id and then by nearest date with values carried forward.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code is as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="2"&gt;data &lt;STRONG&gt;DATAONE&lt;/STRONG&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;infile datalines dlm=' ' truncover;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="2"&gt;input id dateone date9. ;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;format dateone date9.;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;datalines;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 27OCT22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 27OCT22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 27OCT22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 28OCT22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 29OCT22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 30OCT22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 31OCT22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 01NOV22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 02NOV22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 03NOV22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="2"&gt;proc print;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="2"&gt;data &lt;STRONG&gt;DATATWO&lt;/STRONG&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;infile datalines dlm=' ' truncover;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;input id datetwo :date9. num ;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;format datetwo date9. ;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;datalines;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 26OCT22 20&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 28OCT22 18&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 03NOV22 19&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 11NOV22 22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="2"&gt;proc print;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;Want: &lt;STRONG&gt;DATATHREE&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;(where dataset three is a merge of dataone with datettwo by unique ID and by datetwo &amp;lt;= dateone and values of datetwo and num are carried forward. Want DATATHREE as shown as below)&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;Obs id dateone datetwo num &lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 1 27OCT2022 26OCT2022 20 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;2 1 27OCT2022 26OCT2022 20 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;3 1 27OCT2022 26OCT2022 20 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;4 1 28OCT2022 28OCT2022 18 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;5 1 29OCT2022 28OCT2022 18 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;6 1 30OCT2022 28OCT2022 18 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;7 1 31OCT2022 28OCT2022 18 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;8 1 01NOV2022 28OCT2022 18 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;9 1 02NOV2022 28OCT2022 18 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;10 1 03NOV2022 03NOV2022 19&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Sun, 25 Jun 2023 15:47:11 GMT</pubDate>
    <dc:creator>Deps</dc:creator>
    <dc:date>2023-06-25T15:47:11Z</dc:date>
    <item>
      <title>merge two datasets by nearest date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merge-two-datasets-by-nearest-date/m-p/882319#M348603</link>
      <description>&lt;P&gt;Hi, I need to merge two datasets by a unique id and then by nearest date with values carried forward.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code is as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="2"&gt;data &lt;STRONG&gt;DATAONE&lt;/STRONG&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;infile datalines dlm=' ' truncover;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="2"&gt;input id dateone date9. ;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;format dateone date9.;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;datalines;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 27OCT22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 27OCT22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 27OCT22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 28OCT22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 29OCT22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 30OCT22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 31OCT22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 01NOV22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 02NOV22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 03NOV22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="2"&gt;proc print;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="2"&gt;data &lt;STRONG&gt;DATATWO&lt;/STRONG&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;infile datalines dlm=' ' truncover;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;input id datetwo :date9. num ;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;format datetwo date9. ;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;datalines;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 26OCT22 20&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 28OCT22 18&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 03NOV22 19&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 11NOV22 22&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="2"&gt;proc print;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;Want: &lt;STRONG&gt;DATATHREE&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;(where dataset three is a merge of dataone with datettwo by unique ID and by datetwo &amp;lt;= dateone and values of datetwo and num are carried forward. Want DATATHREE as shown as below)&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;Obs id dateone datetwo num &lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;1 1 27OCT2022 26OCT2022 20 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;2 1 27OCT2022 26OCT2022 20 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;3 1 27OCT2022 26OCT2022 20 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;4 1 28OCT2022 28OCT2022 18 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;5 1 29OCT2022 28OCT2022 18 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;6 1 30OCT2022 28OCT2022 18 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;7 1 31OCT2022 28OCT2022 18 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;8 1 01NOV2022 28OCT2022 18 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;9 1 02NOV2022 28OCT2022 18 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="2"&gt;10 1 03NOV2022 03NOV2022 19&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 25 Jun 2023 15:47:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merge-two-datasets-by-nearest-date/m-p/882319#M348603</guid>
      <dc:creator>Deps</dc:creator>
      <dc:date>2023-06-25T15:47:11Z</dc:date>
    </item>
    <item>
      <title>Re: merge two datasets by nearest date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merge-two-datasets-by-nearest-date/m-p/882320#M348604</link>
      <description>&lt;P&gt;This works, but I would not recommend this if you have large data sets. In that case, another approach would be needed, perhaps Hash objects, but I will leave that to others.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table want as select a.*,b.datetwo,b.num
	from dataone as a,datatwo as b
    where a.id=b.id and b.datetwo-a.dateone&amp;lt;=0
	group by a.id,a.dateone
	having (b.datetwo-a.dateone) = max(b.datetwo-a.dateone);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 25 Jun 2023 16:11:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merge-two-datasets-by-nearest-date/m-p/882320#M348604</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-06-25T16:11:49Z</dc:date>
    </item>
    <item>
      <title>Re: merge two datasets by nearest date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merge-two-datasets-by-nearest-date/m-p/882322#M348606</link>
      <description>&lt;P&gt;Don't know any efficient way to find the NEAREST date.&lt;/P&gt;
&lt;P&gt;But if you want the value from the &lt;STRONG&gt;current or most recent date&lt;/STRONG&gt; then just &lt;STRONG&gt;interleave&lt;/STRONG&gt; the datasets by ID and DATE and remember the most recent values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set 
      datatwo(in=in2 rename=(datetwo=dateone num=num2)) 
      dataone(in=in1)
  ;
  by id dateone;
  retain datetwo num ;
  format datetwo date9.;
  drop num2;
  if first.id then call missing(datetwo,num);
  if in2 then do;
    datetwo=dateone;
    num=num2;
  end;
  if in1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs    id      dateone      datetwo    num

  1     1    27OCT2022    26OCT2022     20
  2     1    27OCT2022    26OCT2022     20
  3     1    27OCT2022    26OCT2022     20
  4     1    28OCT2022    28OCT2022     18
  5     1    29OCT2022    28OCT2022     18
  6     1    30OCT2022    28OCT2022     18
  7     1    31OCT2022    28OCT2022     18
  8     1    01NOV2022    28OCT2022     18
  9     1    02NOV2022    28OCT2022     18
 10     1    03NOV2022    03NOV2022     19
&lt;/PRE&gt;</description>
      <pubDate>Sun, 25 Jun 2023 16:38:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merge-two-datasets-by-nearest-date/m-p/882322#M348606</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-25T16:38:14Z</dc:date>
    </item>
  </channel>
</rss>

