<?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 Find the latest date before  a specific date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-the-latest-date-before-a-specific-date/m-p/708246#M217621</link>
    <description>&lt;P&gt;Hi all&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best wishes to all of you for 2021&lt;/P&gt;&lt;P&gt;I am trying to find a simple solution&amp;nbsp; for the the data I have&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input cusip_id Name $10. date_issue :ddmmyy10. datadate :ddmmyy10.  ;
format date_issue ddmmyy10.;
format datadate ddmmyy10.;
datalines;
10343452 Henry PLC 01/04/2014  31/12/2012
10343452 Henry PLC 01/04/2014  31/12/2013
10343452 Henry PLC 01/04/2014  31/12/2014
10343452 Henry PLC 01/04/2014  31/12/2015
10343452 Henry PLC 01/04/2014  31/12/2016
;
run;
/*WHAT I WANT*/
data want ;
input cusip_id Name $10. date_issue :ddmmyy10. datadate :ddmmyy10.  ;
format date_issue ddmmyy10.;
format datadate ddmmyy10.;
datalines;
10343452 Henry PLC 01/04/2014  31/12/2013
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My solution:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
     set have;
	 If dat_issue&amp;gt;=datadate;
run;
proc sort data=want out=want;
     by cusip descending datadate ;
run;
proc sort data=want out=want nodupkeys;
     by cusip  ;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Is there&amp;nbsp; a way to be efficient and have this in a single step. Can this be done in proc sql when I join the cusip and issue_date with the dataset of compustat (that includes all the fiscal years i.ie datadate)?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks in advance&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;George&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 26 Dec 2020 07:27:50 GMT</pubDate>
    <dc:creator>georgel</dc:creator>
    <dc:date>2020-12-26T07:27:50Z</dc:date>
    <item>
      <title>Find the latest date before  a specific date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-latest-date-before-a-specific-date/m-p/708246#M217621</link>
      <description>&lt;P&gt;Hi all&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best wishes to all of you for 2021&lt;/P&gt;&lt;P&gt;I am trying to find a simple solution&amp;nbsp; for the the data I have&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input cusip_id Name $10. date_issue :ddmmyy10. datadate :ddmmyy10.  ;
format date_issue ddmmyy10.;
format datadate ddmmyy10.;
datalines;
10343452 Henry PLC 01/04/2014  31/12/2012
10343452 Henry PLC 01/04/2014  31/12/2013
10343452 Henry PLC 01/04/2014  31/12/2014
10343452 Henry PLC 01/04/2014  31/12/2015
10343452 Henry PLC 01/04/2014  31/12/2016
;
run;
/*WHAT I WANT*/
data want ;
input cusip_id Name $10. date_issue :ddmmyy10. datadate :ddmmyy10.  ;
format date_issue ddmmyy10.;
format datadate ddmmyy10.;
datalines;
10343452 Henry PLC 01/04/2014  31/12/2013
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My solution:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
     set have;
	 If dat_issue&amp;gt;=datadate;
run;
proc sort data=want out=want;
     by cusip descending datadate ;
run;
proc sort data=want out=want nodupkeys;
     by cusip  ;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Is there&amp;nbsp; a way to be efficient and have this in a single step. Can this be done in proc sql when I join the cusip and issue_date with the dataset of compustat (that includes all the fiscal years i.ie datadate)?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks in advance&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;George&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 26 Dec 2020 07:27:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-latest-date-before-a-specific-date/m-p/708246#M217621</guid>
      <dc:creator>georgel</dc:creator>
      <dc:date>2020-12-26T07:27:50Z</dc:date>
    </item>
    <item>
      <title>Re: Find the latest date before  a specific date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-latest-date-before-a-specific-date/m-p/708254#M217625</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select  *,monotonic() as row_num from have where date_issue ge datadate group by cusip_id having max(row_num)=row_num;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 26 Dec 2020 09:20:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-latest-date-before-a-specific-date/m-p/708254#M217625</guid>
      <dc:creator>singhsahab</dc:creator>
      <dc:date>2020-12-26T09:20:12Z</dc:date>
    </item>
    <item>
      <title>Re: Find the latest date before  a specific date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-latest-date-before-a-specific-date/m-p/708255#M217626</link>
      <description>&lt;P&gt;If your dataset is already sorted in ascending order, a double DO loop does it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do until (last.cusip);
  set have;
  by cusip;
  if datadate le date_issue then ref = datadate;
end;
do until (last.cusip);
  set have;
  by cusip;
  if datadate = ref then output;
end;
drop ref;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In SQL, try this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select *
  from have
  where datadate le date_issue
  group by cusip
  having date_issue - datadate = min(date_issue - datadate)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, posted from my tablet.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 26 Dec 2020 09:30:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-latest-date-before-a-specific-date/m-p/708255#M217626</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-12-26T09:30:15Z</dc:date>
    </item>
    <item>
      <title>Re: Find the latest date before  a specific date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-latest-date-before-a-specific-date/m-p/708256#M217638</link>
      <description>This is exactly what I wanted. Great Many thanks&lt;BR /&gt;&lt;BR /&gt;Regards&lt;BR /&gt;George</description>
      <pubDate>Sat, 26 Dec 2020 09:39:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-latest-date-before-a-specific-date/m-p/708256#M217638</guid>
      <dc:creator>georgel</dc:creator>
      <dc:date>2020-12-26T09:39:38Z</dc:date>
    </item>
    <item>
      <title>Re: Find the latest date before  a specific date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-latest-date-before-a-specific-date/m-p/708258#M217640</link>
      <description>Dear Kurt&lt;BR /&gt;Many thanks for this solution and for your quick response. I really appreciated your help&lt;BR /&gt;Regards&lt;BR /&gt;George</description>
      <pubDate>Sat, 26 Dec 2020 09:44:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-latest-date-before-a-specific-date/m-p/708258#M217640</guid>
      <dc:creator>georgel</dc:creator>
      <dc:date>2020-12-26T09:44:23Z</dc:date>
    </item>
  </channel>
</rss>

