<?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 to assign values by comparing dates in two data sets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-values-by-comparing-dates-in-two-data-sets/m-p/292631#M60781</link>
    <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data WANT;
  merge EX AE end=LASTOBS;      
  by SUBID ;
  if ^LASTOBS then set EX(keep=SUBID EXDATE rename=(SUBID=SUBID_NEXT EXDATE=EXDATE_NEXT) firstobs=2);
  if EXCAT='Study drug first taken' then READY+1;
  if READY                     %* we have found 'first taken' string       ;
   &amp;amp; ( EXDATE_NEXT &amp;gt;= AEDATE   %* and the next record is too late        ;
     | SUBID_NEXT ne SUBID     %*   (either date too large or new SUBID) ;
     | LASTOBS )               %*    or last observation)                ;
     then do;                  %* now is the time to write the record for this SUBID;
       if EXDATE &amp;lt; AEDATE then LASTDOSE=EXDOSE; 
       output;
       READY=0;
  end;
  keep SUBID AEDATE LASTDOSE;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE class="table" rules="all" frame="box" cellspacing="0" cellpadding="5" summary="Procedure Print: Data Set WORK.T"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="l header" scope="col"&gt;Subid&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;AEDATE&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;LASTDOSE&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;18FEB2016&lt;/TD&gt;
&lt;TD class="r data"&gt;10&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;11JAN2016&lt;/TD&gt;
&lt;TD class="r data"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
    <pubDate>Fri, 19 Aug 2016 03:32:22 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2016-08-19T03:32:22Z</dc:date>
    <item>
      <title>How to assign values by comparing dates in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-values-by-comparing-dates-in-two-data-sets/m-p/292573#M60759</link>
      <description>&lt;P&gt;Dear,&lt;/P&gt;&lt;P&gt;I have two datasets. One EX and other AE. I need create a variable called LASTDOSE in my AE dataset by comparing AEdate with exdate.&lt;/P&gt;&lt;P&gt;Some one from helped me previously. But I need to fix one more thing in that code. Please help.&lt;/P&gt;&lt;P&gt;EX&lt;/P&gt;&lt;P&gt;Subid &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;exdate &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;exdose &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;excat&lt;BR /&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;07FEB2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PSG night&lt;BR /&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 17FEB2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Study drug first taken &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;BR /&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;27FEB2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;20&lt;BR /&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 07JAN2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PSG night&lt;BR /&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;17JAN2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Study drug first taken&lt;BR /&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 17JAN2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 40&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;AE&lt;/P&gt;&lt;P&gt;Subid &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AEDATE&lt;BR /&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;18FEB2016&lt;BR /&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;11JAN2016&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output needed.&lt;/P&gt;&lt;P&gt;Subid &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; AEDATE &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; Lastdose&lt;BR /&gt;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; 18FEB2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&lt;BR /&gt;2 &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;11JAN2016 &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;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For SUBID=1 10 is the last dose taken before AEdate&amp;nbsp;&lt;BR /&gt;For SUBID=2, &amp;nbsp;the last dose taken before AEdate is blank because study drug date first taken(&lt;SPAN&gt;17JAN2016&lt;/SPAN&gt;) has to be considered. So, the last dose is blank.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to consider the doses taken on or after excat='study first drug taken'..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Please help in my code&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Ouput by my code;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Subid &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; AEDATE &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; Lastdose&lt;BR /&gt;&lt;SPAN&gt;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; 18FEB2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2 &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;11JAN2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20&lt;/SPAN&gt;&lt;/SPAN&gt;&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;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;proc&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;sql&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token statement"&gt;select&lt;/SPAN&gt; a&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;b&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;ex&lt;/SPAN&gt;dose
 &lt;SPAN class="token keyword"&gt;from&lt;/SPAN&gt; AE as a &lt;SPAN class="token function"&gt;left&lt;/SPAN&gt; join &lt;SPAN class="token number"&gt;EX&lt;/SPAN&gt; as b 
  on a&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;Subid&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;b&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;Subid and a&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;AEDATE&lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt;b&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;ex&lt;/SPAN&gt;date &lt;SPAN class="token operator"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;
    &lt;SPAN class="token keyword"&gt;group&lt;/SPAN&gt; &lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; a&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;Subid&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;a&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;AEDATE 
     having a&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;AEDATE&lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt;b&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;ex&lt;/SPAN&gt;date&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;min&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;a&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;AEDATE&lt;SPAN class="token operator"&gt;-&lt;/SPAN&gt;b&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;ex&lt;/SPAN&gt;date&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;quit&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2016 21:15:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-values-by-comparing-dates-in-two-data-sets/m-p/292573#M60759</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2016-08-18T21:15:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign values by comparing dates in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-values-by-comparing-dates-in-two-data-sets/m-p/292619#M60773</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please try to retain the first dose date related to study in a new variable and then back merge that dataset with the source EX dataset on subid.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data firstdose;
set ex (where=(excat='Study drug first taken'));
by subid;
if first.subid;
rename exdate=firstdosdt;
keep Subid exdate;
run;

data ex_;
merge ex(in=a) firstdose(in=b);
by subid;
if a;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then , use this dataset in the proc sql as below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;&lt;BR /&gt;select a.*,b.exdose&lt;BR /&gt;&amp;nbsp;from AE as a left join EX_ as b &lt;BR /&gt;&amp;nbsp; on a.Subid=b.Subid and a.AEDATE-b.firstdosdt &amp;gt;0&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; group by a.Subid,a.AEDATE &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; having b.firstdosdt=b.exdate and a.AEDATE-b.firstdosdt=min(a.AEDATE-b.firstdosdt) ;&lt;BR /&gt;quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope this helps.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Aug 2016 01:48:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-values-by-comparing-dates-in-two-data-sets/m-p/292619#M60773</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2016-08-19T01:48:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign values by comparing dates in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-values-by-comparing-dates-in-two-data-sets/m-p/292627#M60778</link>
      <description>&lt;P&gt;Sequential operations are better handled with a data step. &amp;nbsp;First, eliminate irrelevant records from EX (before drug is taken). Then interleave the two datasets. Finally, process the sequence of dates for each subId.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data EXtaken;
do until(last.subId);
    set ex; 
    by subId exdate; /* Optional, just checking */
    if not taken then taken = upcase(excat) = "STUDY DRUG FIRST TAKEN";
    if taken then output;
    end;
run;

proc sql;
create table seq as
select * from EXtaken
union all corr
select subId, aeDate as exDate, . as exDose from AE
order by subId, exDate;
quit;

data want;
do until(last.subId);
    set seq; by subId;
    if missing(exDose) then output;
    else lastDose = exDose;
    end;
keep subId exDate lastDose;
rename exDate=AEDATE;
run;

proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 Aug 2016 02:42:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-values-by-comparing-dates-in-two-data-sets/m-p/292627#M60778</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-08-19T02:42:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign values by comparing dates in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-values-by-comparing-dates-in-two-data-sets/m-p/292629#M60779</link>
      <description>&lt;P&gt;Thank you for your help. Your code gives the 'first study dose taken' &amp;nbsp;dose values for all observation. &amp;nbsp;But I need most recent dose taken prior to aedate.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I modified your code. Please check if this is ok and let me know if anything wrong. Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table one as&lt;BR /&gt;select a.*,b.exdose,exdate,firstdosdt&lt;BR /&gt;from AE as a left join EX_ as b&lt;BR /&gt;on a.Subid=b.Subid and a.AEDATE-b.firstdosdt &amp;gt;0 and a.AEDATE-b.exdate &amp;gt;0&lt;BR /&gt;group by a.Subid,a.AEDATE&lt;BR /&gt;having a.AEDATE-b.exdate=min(a.AEDATE-b.exdate); ;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Aug 2016 03:04:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-values-by-comparing-dates-in-two-data-sets/m-p/292629#M60779</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2016-08-19T03:04:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign values by comparing dates in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-values-by-comparing-dates-in-two-data-sets/m-p/292630#M60780</link>
      <description>&lt;PRE&gt;
How about this one :


data ex;
infile cards truncover;
input Subid        exdate  : date9.        exdose                    excat $100.;
format exdate  date9. ;
cards;
1              07FEB2016          5                        PSG night
1             17FEB2016         10                      Study drug first taken                    
1              27FEB2016        20
2             07JAN2016         20                        PSG night
2            17JAN2016          30                      Study drug first taken
2           17JAN2016           40
;
run;

data ae;
input Subid                AEDATE : date9.;
format  AEDATE date9.;
cards;
1                    18FEB2016
2                    11JAN2016
;
run;

proc sql;
select a.*,case when(sum(excat='Study drug first taken') ne 0) then exdose
 else . end as last_dose
 from ae as a left join ex as b
  on a.subid=b.subid and a.AEDATE ge b.exdate
   group by a.subid,a.AEDATE
    having b.exdate=max(b.exdate);
quit;


&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 Aug 2016 03:12:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-values-by-comparing-dates-in-two-data-sets/m-p/292630#M60780</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-08-19T03:12:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign values by comparing dates in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-values-by-comparing-dates-in-two-data-sets/m-p/292631#M60781</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data WANT;
  merge EX AE end=LASTOBS;      
  by SUBID ;
  if ^LASTOBS then set EX(keep=SUBID EXDATE rename=(SUBID=SUBID_NEXT EXDATE=EXDATE_NEXT) firstobs=2);
  if EXCAT='Study drug first taken' then READY+1;
  if READY                     %* we have found 'first taken' string       ;
   &amp;amp; ( EXDATE_NEXT &amp;gt;= AEDATE   %* and the next record is too late        ;
     | SUBID_NEXT ne SUBID     %*   (either date too large or new SUBID) ;
     | LASTOBS )               %*    or last observation)                ;
     then do;                  %* now is the time to write the record for this SUBID;
       if EXDATE &amp;lt; AEDATE then LASTDOSE=EXDOSE; 
       output;
       READY=0;
  end;
  keep SUBID AEDATE LASTDOSE;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE class="table" rules="all" frame="box" cellspacing="0" cellpadding="5" summary="Procedure Print: Data Set WORK.T"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="l header" scope="col"&gt;Subid&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;AEDATE&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;LASTDOSE&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;18FEB2016&lt;/TD&gt;
&lt;TD class="r data"&gt;10&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;11JAN2016&lt;/TD&gt;
&lt;TD class="r data"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Fri, 19 Aug 2016 03:32:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-values-by-comparing-dates-in-two-data-sets/m-p/292631#M60781</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2016-08-19T03:32:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign values by comparing dates in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-values-by-comparing-dates-in-two-data-sets/m-p/292633#M60783</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I agree with your code and its generating the right output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also the below code works&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;&lt;BR /&gt;select a.*,b.exdose&lt;BR /&gt;&amp;nbsp;from AE as a left join EX_ as b &lt;BR /&gt;&amp;nbsp; on a.Subid=b.Subid and a.AEDATE-b.firstdosdt &amp;gt;0&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; group by a.Subid,a.AEDATE &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; having &lt;STRONG&gt;b.firstdosdt=b.exdate&lt;/STRONG&gt; and a.AEDATE-b.firstdosdt=min(a.AEDATE-b.firstdosdt) ;&lt;BR /&gt;quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 Aug 2016 03:24:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-values-by-comparing-dates-in-two-data-sets/m-p/292633#M60783</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2016-08-19T03:24:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to assign values by comparing dates in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-assign-values-by-comparing-dates-in-two-data-sets/m-p/292634#M60784</link>
      <description>&lt;P&gt;When several alternatives are possible, you must consider the legibility (how easy is it to follow the&amp;nbsp;intent and the logic) and performance.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Aug 2016 03:31:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-assign-values-by-comparing-dates-in-two-data-sets/m-p/292634#M60784</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2016-08-19T03:31:14Z</dc:date>
    </item>
  </channel>
</rss>

