<?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 Proc sql - join on relative dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-join-on-relative-dates/m-p/591465#M169424</link>
    <description>&lt;P&gt;EDIT: the code at the end works, I've just made a mistake in the example. Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp; for pointing this out&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have two datasets (ds1 and ds2), each of them containg date variable. Now I want to join them using the condition that date in ds2 is less than 30 days before or 30 days after the date in ds1. In the example below, in the outcome dataset (want) observations for B are not matched as there are more than 30 days between 2018-12-25 and 2019-04-20. For A and C the condition is met, so the observations from ds2 should be added.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds1;
	input date :yymmdd10. id $2. var1;
	format date yymmdd10.;
	datalines;
	2019-03-31 A 10
	2018-12-25 B 20
	2019-06-15 C 30
	;
run;

data ds2;
	input date :yymmdd10. id $2. var2;
	format date yymmdd10.;
	datalines;
	2019-02-25 A 15
	2019-04-20 B 25
	2019-07-10 C 35
	;
run;

data want;
	input date :yymmdd10. id :$2. var1 var2;
	format date yymmdd10.;
	datalines;
	2019-02-25 A 10 15
	2019-04-20 B 20 .
	2019-07-10 C 30 35
	;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I've tried the following code, but values for id=A are not matched while they should be (there is less than 30 days between 2019-02-25 and 2019-03-31). C is mergin properly. What am I doing wrong?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table merge as
	select * from ds1 A
	left join ds2 B
	on A.id=B.id and B.date &amp;lt; A.date+30 and B.date&amp;gt; A.date-30;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;P&gt;Chris&lt;/P&gt;</description>
    <pubDate>Wed, 25 Sep 2019 12:10:36 GMT</pubDate>
    <dc:creator>chris2377</dc:creator>
    <dc:date>2019-09-25T12:10:36Z</dc:date>
    <item>
      <title>Proc sql - join on relative dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-join-on-relative-dates/m-p/591465#M169424</link>
      <description>&lt;P&gt;EDIT: the code at the end works, I've just made a mistake in the example. Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp; for pointing this out&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have two datasets (ds1 and ds2), each of them containg date variable. Now I want to join them using the condition that date in ds2 is less than 30 days before or 30 days after the date in ds1. In the example below, in the outcome dataset (want) observations for B are not matched as there are more than 30 days between 2018-12-25 and 2019-04-20. For A and C the condition is met, so the observations from ds2 should be added.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds1;
	input date :yymmdd10. id $2. var1;
	format date yymmdd10.;
	datalines;
	2019-03-31 A 10
	2018-12-25 B 20
	2019-06-15 C 30
	;
run;

data ds2;
	input date :yymmdd10. id $2. var2;
	format date yymmdd10.;
	datalines;
	2019-02-25 A 15
	2019-04-20 B 25
	2019-07-10 C 35
	;
run;

data want;
	input date :yymmdd10. id :$2. var1 var2;
	format date yymmdd10.;
	datalines;
	2019-02-25 A 10 15
	2019-04-20 B 20 .
	2019-07-10 C 30 35
	;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I've tried the following code, but values for id=A are not matched while they should be (there is less than 30 days between 2019-02-25 and 2019-03-31). C is mergin properly. What am I doing wrong?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table merge as
	select * from ds1 A
	left join ds2 B
	on A.id=B.id and B.date &amp;lt; A.date+30 and B.date&amp;gt; A.date-30;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;P&gt;Chris&lt;/P&gt;</description>
      <pubDate>Wed, 25 Sep 2019 12:10:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-join-on-relative-dates/m-p/591465#M169424</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2019-09-25T12:10:36Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql - join on relative dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-join-on-relative-dates/m-p/591468#M169425</link>
      <description>&lt;P&gt;Comparison:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ods listing;
data test;
	input date :yymmdd10. id $2. date1 :yymmdd10.;
	format date date1 yymmdd10.;
  diff = date - date1;
	datalines;
	2019-03-31 A 2019-02-25
	2018-12-25 B 2019-04-20
	2019-06-15 C 2019-07-10
	;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;returns:&lt;/P&gt;&lt;PRE&gt;Obs          date    id         date1    diff

 1     2019-03-31    A     2019-02-25      34
 2     2018-12-25    B     2019-04-20    -116
 3     2019-06-15    C     2019-07-10     -25&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All the best&lt;/P&gt;&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Wed, 25 Sep 2019 12:02:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-join-on-relative-dates/m-p/591468#M169425</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2019-09-25T12:02:56Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql - join on relative dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-join-on-relative-dates/m-p/591471#M169426</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;All clear now, thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 25 Sep 2019 12:09:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-join-on-relative-dates/m-p/591471#M169426</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2019-09-25T12:09:04Z</dc:date>
    </item>
  </channel>
</rss>

