<?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: Left join on dates within threshold - two datasets in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Left-join-on-dates-within-threshold-two-datasets/m-p/644073#M21950</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/281550"&gt;@sustagens&lt;/a&gt;,&amp;nbsp;thank you so much for your reply and your solution! I will try that now, and play around with it to get the outcome.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much, I really, really,&amp;nbsp;&lt;STRONG&gt;REALLY&amp;nbsp;&lt;/STRONG&gt;appreciate your help!&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 29 Apr 2020 20:22:32 GMT</pubDate>
    <dc:creator>mugsie91</dc:creator>
    <dc:date>2020-04-29T20:22:32Z</dc:date>
    <item>
      <title>Left join on dates within threshold - two datasets</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Left-join-on-dates-within-threshold-two-datasets/m-p/643757#M21935</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am hoping you can help me! **Please help!!!!**&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":folded_hands:"&gt;🙏&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I am in SAS and I have datasets A and B with different measurements (relating to patient's health) as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Dataset A&lt;/STRONG&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;measurement_a&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20JUN2013&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 52.3&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 12JUL2013&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;65.6&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 28NOV2014&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 37.4&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 02DEC2014&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 61.3&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 22SEP2015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 40.5&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;15OCT2015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;60.5&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;03JUN2011&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;46.5 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;19JUL2011&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 54.1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;29OCT2012&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;53.6&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;Dataset B&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; measurement_b&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 21MAR2007&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 43&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 13JUL2007&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;45&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;07APR2009&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;47&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;14MAY2009&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;46&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;16FEB2012&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 42&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;27AUG2012&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;53&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12DEC2012&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;58 &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20JUN2013&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 56&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10DEC2013&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 53&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;23MAY2014&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 49&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;17SEP2014&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 44&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;23SEP2015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 40&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;16DEC2011&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 58&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;22AUG2012&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;54&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20FEB2013&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 56&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;29MAY2013&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;53&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;What I am looking for is that if the date in Dataset B is within 6 months of the date in Dataset a, then a new variable called "time" will be added, saying 1,2,3,etc. for how many ever match with ** only measurement_a** length (in other words, I do not need to retain values of measurement_b if it does not match the date in Dataset a. Here is an example of what I mean:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Desired result/dataset&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; Time&amp;nbsp; &amp;nbsp; measure_a&amp;nbsp; &amp;nbsp; measure_b &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;52.3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;56 (Dataset B Date = 20JUN2013 - Matched exactly)&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;65.6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;53 (Dataset B date = 10DEC2013 - Within six months of 12JUL2013 [Dataset A Date])&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;37.4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;44 (Dataset B date = 17SEP2014 - Within six months of 28NOV2014 [Dataset A Date])&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;61.3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; . (because 17SEP2014 [Dataset B] is closest to 28NOV2014 [Dataset A]) &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;40.5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;40 (because 23SEP2015 [Dataset B] is closest to 22SEP2015 [Dataset A])&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;60.5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; . (No date in Dataset B that is within 6 months of Date in Dataset A [15OCT2015])&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;46.5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; . (See below)&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;54.1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 58 (because 03JUL2011 [Dataset B] is closest to 19JUL2011 [Dataset A]) &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;53.6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 54 (Dataset B date = 22AUG2012 - Within 6 months of Dataset A date = 29OCT2012)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have joined on ID but the times is proving difficult. I know it could be the difference in months in the "where" statement in the following code:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;PROC SQL;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;CREATE TABLE join_test as&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;SELECT * FROM data_a as a&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;LEFT_JOIN data_b as b&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;ON a.id = b.id &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;WHERE days(a.Date - b.Date) &amp;lt;= 180 ;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="2"&gt;QUIT;&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;But this does not do the trick.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Can some **please** help me?&lt;/P&gt;&lt;P&gt;I really appreciate it. Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 03:54:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Left-join-on-dates-within-threshold-two-datasets/m-p/643757#M21935</guid>
      <dc:creator>mugsie91</dc:creator>
      <dc:date>2020-04-29T03:54:15Z</dc:date>
    </item>
    <item>
      <title>Re: Left join on dates within threshold - two datasets</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Left-join-on-dates-within-threshold-two-datasets/m-p/643762#M21936</link>
      <description>&lt;P&gt;If you read the log you should have an error about an apparent function DAYS not existing. SAS SQL does not have a "days" function. You can get the number of days between two date values by 1) subtraction or 2) use of the INTCK function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By "6 months" do you mean "6 calendar months" as in 1 January is "6 months" before "30 July " [month 7 - month 1=6] or do you actually want 180 days? There is a difference. Months have differing numbers of days and the exact comparison you expect needs to be defined.&lt;/P&gt;
&lt;P&gt;Also, you need to consider order. Any value that precedes another when using subtraction will yield a negative number and hence is "&amp;lt;180". So are you only concerned with dates in B greater than the dates in A?&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 05:01:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Left-join-on-dates-within-threshold-two-datasets/m-p/643762#M21936</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-04-29T05:01:30Z</dc:date>
    </item>
    <item>
      <title>Re: Left join on dates within threshold - two datasets</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Left-join-on-dates-within-threshold-two-datasets/m-p/643773#M21937</link>
      <description>&lt;P&gt;What if several measurements from B fall into the 6-month range of a particular observation in A? What is the rule for selecting only one?&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 06:32:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Left-join-on-dates-within-threshold-two-datasets/m-p/643773#M21937</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-29T06:32:26Z</dc:date>
    </item>
    <item>
      <title>Re: Left join on dates within threshold - two datasets</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Left-join-on-dates-within-threshold-two-datasets/m-p/643836#M21939</link>
      <description>&lt;P&gt;Looks like there are several scenarios that need to be handled&lt;/P&gt;
&lt;P&gt;1. If the date in dataset B is &lt;U&gt;earlier&lt;/U&gt; by 6 months or less&lt;/P&gt;
&lt;P&gt;2. If the date in dataset B is &lt;U&gt;later&lt;/U&gt; by 6 months or less&lt;/P&gt;
&lt;P&gt;3. If the date in dataset B is &lt;U&gt;exactly&lt;/U&gt; the same date&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We need to define their order of precedence as there are entries that can fit two of the above scenarios, so we can decide what will be the final "measurement_b" value to take.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is what I have so far, you can play around with the order of the if-then handling below to suit what outcome you want to achieve&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds1;
  set ds1;
  time + 1; /*add counter*/
  by id date;
  if first.id then time = 1;
run;

data ds;
set ds1 ds2; /*combine so they can be sorted by date*/
run;

proc sort data=ds;
by id date; /*sorting*/
run;

data look_both_ways (keep=id date measurement_a time keep rename=(keep=measurement_b) where=(time ne .));
set ds;
by ID;
set ds (firstobs=2 keep=date measurement_b rename=(date=next_date measurement_b=nxt_m_b))
    ds (obs=1 keep=time);
format prev_date date9.;

prev_date=ifn(first.ID,(.),lag(date));
next_date=ifn(last.ID,(.),next_date);

prev_m_b = ifn( first.Id, (.), lag(measurement_b));
nxt_m_b = ifn(last.Id, (.), nxt_m_b);

if intck('month',prev_date,date) &amp;lt;= 6 then keep=prev_m_b; /*scenario 1*/
if intck('month',date,next_date) &amp;lt;= 6 then keep=nxt_m_b; /*scenario 2*/
if date=next_date then keep=nxt_m_b; /*scenario 3*/
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Apr 2020 08:50:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Left-join-on-dates-within-threshold-two-datasets/m-p/643836#M21939</guid>
      <dc:creator>sustagens</dc:creator>
      <dc:date>2020-04-29T08:50:37Z</dc:date>
    </item>
    <item>
      <title>Re: Left join on dates within threshold - two datasets</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Left-join-on-dates-within-threshold-two-datasets/m-p/644071#M21948</link>
      <description>&lt;P&gt;You're absolutely right about the days - thank you. I will use the 'intck' function from now on.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;By 6 months i mean 180 days actually, since I feel as though it is a more holistic capture of a patient.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That is an interesting question and something I did not consider, actually. Now that I think about it, I do not need dates in B after dates in A - I want any date in Dataset B that falls within +6 months or -6 months of the date in Dataset A. So I would use an absolute value for this!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for these questions. These are exactly what I need to consider! Thank you so much for your reply!&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 20:19:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Left-join-on-dates-within-threshold-two-datasets/m-p/644071#M21948</guid>
      <dc:creator>mugsie91</dc:creator>
      <dc:date>2020-04-29T20:19:18Z</dc:date>
    </item>
    <item>
      <title>Re: Left join on dates within threshold - two datasets</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Left-join-on-dates-within-threshold-two-datasets/m-p/644072#M21949</link>
      <description>&lt;P&gt;Hi there, thank you for your reply. If there are many measurements, the general rule of thumb is is to select the date in Dataset B that is the&amp;nbsp;&lt;STRONG&gt;closest&lt;/STRONG&gt; to the date in Dataset A.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 20:20:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Left-join-on-dates-within-threshold-two-datasets/m-p/644072#M21949</guid>
      <dc:creator>mugsie91</dc:creator>
      <dc:date>2020-04-29T20:20:59Z</dc:date>
    </item>
    <item>
      <title>Re: Left join on dates within threshold - two datasets</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Left-join-on-dates-within-threshold-two-datasets/m-p/644073#M21950</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/281550"&gt;@sustagens&lt;/a&gt;,&amp;nbsp;thank you so much for your reply and your solution! I will try that now, and play around with it to get the outcome.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much, I really, really,&amp;nbsp;&lt;STRONG&gt;REALLY&amp;nbsp;&lt;/STRONG&gt;appreciate your help!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Apr 2020 20:22:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Left-join-on-dates-within-threshold-two-datasets/m-p/644073#M21950</guid>
      <dc:creator>mugsie91</dc:creator>
      <dc:date>2020-04-29T20:22:32Z</dc:date>
    </item>
    <item>
      <title>Re: Left join on dates within threshold - two datasets</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Left-join-on-dates-within-threshold-two-datasets/m-p/644156#M21959</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/325956"&gt;@mugsie91&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi there, thank you for your reply. If there are many measurements, the general rule of thumb is is to select the date in Dataset B that is the&amp;nbsp;&lt;STRONG&gt;closest&lt;/STRONG&gt; to the date in Dataset A.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then you should try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
input ID $ Date :date9. measurement_a;
format date yymmddd10.;
datalines;
1 20JUN2013 52.3
1 12JUL2013 65.6
1 28NOV2014 37.4
1 02DEC2014 61.3
1 22SEP2015 40.5
1 15OCT2015 60.5
2 03JUN2011 46.5 
2 19JUL2011 54.1
2 29OCT2012 53.6
;

data b;
input ID $ Date :date9. measurement_b;
format date yymmddd10.;
datalines;
1 21MAR2007 43
1 13JUL2007 45
1 07APR2009 47
1 14MAY2009 46
1 16FEB2012 42
1 27AUG2012 53
1 12DEC2012 58 
1 20JUN2013 56
1 10DEC2013 53
1 23MAY2014 49
1 17SEP2014 44
1 23SEP2015 40
2 16DEC2011 58
2 22AUG2012 54
2 20FEB2013 56
2 29MAY2013 53
;

proc sql;
create table want as
  select
    a.id,
    a.date,
    b.date as b_date,
    a.measurement_a,
    b.measurement_b,
    abs(a.date - b.date) as datdif
  from a
  left join b
  on a.id = b.id and abs(a.date - b.date) le 180
  group by a.id, a.date
  having datdif = min(datdif)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Apr 2020 06:30:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Left-join-on-dates-within-threshold-two-datasets/m-p/644156#M21959</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-30T06:30:28Z</dc:date>
    </item>
    <item>
      <title>Re: Left join on dates within threshold - two datasets</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Left-join-on-dates-within-threshold-two-datasets/m-p/645132#M22023</link>
      <description>&lt;P&gt;Hello - thank you so much! This does the job for me. Thank you so much, I really appreciate it!&lt;/P&gt;</description>
      <pubDate>Mon, 04 May 2020 23:27:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Left-join-on-dates-within-threshold-two-datasets/m-p/645132#M22023</guid>
      <dc:creator>mugsie91</dc:creator>
      <dc:date>2020-05-04T23:27:03Z</dc:date>
    </item>
  </channel>
</rss>

