<?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: To find out previous date out of one set. in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/To-find-out-previous-date-out-of-one-set/m-p/440447#M13585</link>
    <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;&lt;BR /&gt;input Id$ Date:yymmdd10.;&lt;BR /&gt;format date yymmdd10.;&lt;BR /&gt;datalines;&lt;BR /&gt;S001 1998/01/23&lt;BR /&gt;S001 1999/01/24&lt;BR /&gt;S001 2000/01/25&lt;BR /&gt;S001 2012/09/25&lt;BR /&gt;S002 1988/01/23&lt;BR /&gt;S002 1999/01/24&lt;BR /&gt;S002 2000/01/24&lt;BR /&gt;S002 2014/01/24&lt;BR /&gt;S002 2015/01/24&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt; set have;&lt;BR /&gt; by Id;&lt;BR /&gt; date=ifn(first.Id, ., lag1(date));&lt;BR /&gt; if last.id;&lt;BR /&gt;run;&lt;/PRE&gt;</description>
    <pubDate>Tue, 27 Feb 2018 11:01:10 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2018-02-27T11:01:10Z</dc:date>
    <item>
      <title>To find out previous date out of one set.</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/To-find-out-previous-date-out-of-one-set/m-p/440444#M13584</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following input dataset which I created out&lt;/P&gt;&lt;P&gt;Of the requirements I got. For each id , I want to output the previous value of date based on last date in that id. The dataset is sorted on Id ascending and date ascending. I have used lag function to get previous date but here the requirement is to output date just one previous to the last entry of date for that id.&lt;/P&gt;&lt;P&gt;Any suggestions or help will be of great help for me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Id.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Date&lt;/P&gt;&lt;P&gt;S001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1998/01/23&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;S001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1999/01/24&lt;/P&gt;&lt;P&gt;S001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2000/01/25&lt;/P&gt;&lt;P&gt;S001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2012/09/25&lt;/P&gt;&lt;P&gt;S002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;1988/01/23&amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;S002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;1999/01/24&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;S002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;&amp;nbsp;2000/01/24&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;S002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;2014/01/24&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;S002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;2015/01/24&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Output should be -&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;S001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2000/01/25&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;S002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2014/01/24&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Feb 2018 10:44:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/To-find-out-previous-date-out-of-one-set/m-p/440444#M13584</guid>
      <dc:creator>Sasforme</dc:creator>
      <dc:date>2018-02-27T10:44:49Z</dc:date>
    </item>
    <item>
      <title>Re: To find out previous date out of one set.</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/To-find-out-previous-date-out-of-one-set/m-p/440447#M13585</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;&lt;BR /&gt;input Id$ Date:yymmdd10.;&lt;BR /&gt;format date yymmdd10.;&lt;BR /&gt;datalines;&lt;BR /&gt;S001 1998/01/23&lt;BR /&gt;S001 1999/01/24&lt;BR /&gt;S001 2000/01/25&lt;BR /&gt;S001 2012/09/25&lt;BR /&gt;S002 1988/01/23&lt;BR /&gt;S002 1999/01/24&lt;BR /&gt;S002 2000/01/24&lt;BR /&gt;S002 2014/01/24&lt;BR /&gt;S002 2015/01/24&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt; set have;&lt;BR /&gt; by Id;&lt;BR /&gt; date=ifn(first.Id, ., lag1(date));&lt;BR /&gt; if last.id;&lt;BR /&gt;run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Feb 2018 11:01:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/To-find-out-previous-date-out-of-one-set/m-p/440447#M13585</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-02-27T11:01:10Z</dc:date>
    </item>
    <item>
      <title>Re: To find out previous date out of one set.</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/To-find-out-previous-date-out-of-one-set/m-p/440476#M13586</link>
      <description>&lt;P&gt;The following code will return all rows with the 2nd latest date per ID.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Id$ Date:yymmdd10.;
  format date yymmdd10.;
  someOtherVar=_n_;
  datalines;
S001 1998/01/23
S001 1999/01/24
S001 2000/01/25
S001 2000/01/25
S001 2012/09/25
S002 1988/01/23
S002 1999/01/24
S002 2000/01/24
S002 2014/01/24
S002 2015/01/24
S002 2015/01/24
;
run;

/* option 1 */
proc sql;
  create table want as
    select *
    from
      (
        select *
        from have
        group by id
        having date ne max(date) 
      )
    group by id
    having date = max(date) 
    ;
quit;

/* option 2*/
proc sort data=have out=want2;
  by id descending date;
run;

data want2(drop=_:);
  set want2;
  by id descending date;
  if first.id then _counter=1;
  else if first.date then _counter+1;
  if _counter=2 then output;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Feb 2018 12:47:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/To-find-out-previous-date-out-of-one-set/m-p/440476#M13586</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-02-27T12:47:21Z</dc:date>
    </item>
    <item>
      <title>Re: To find out previous date out of one set.</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/To-find-out-previous-date-out-of-one-set/m-p/440481#M13587</link>
      <description>&lt;P&gt;IF you have other variables need to retain.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Id$ Date:yymmdd10.;
format date yymmdd10.;
datalines;
S001 1998/01/23
S001 1999/01/24
S001 2000/01/25
S001 2012/09/25
S002 1988/01/23
S002 1999/01/24
S002 2000/01/24
S002 2014/01/24
S002 2015/01/24
;
data temp;
 set have;
 by id;
 if last.id then delete;
run;
data want;
 set temp;
 by id;
 if last.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Feb 2018 12:44:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/To-find-out-previous-date-out-of-one-set/m-p/440481#M13587</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-02-27T12:44:34Z</dc:date>
    </item>
    <item>
      <title>Re: To find out previous date out of one set.</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/To-find-out-previous-date-out-of-one-set/m-p/440492#M13588</link>
      <description>Thanks for your reply.&lt;BR /&gt;What is the meaning of date = ifn? I am feeling this for first time</description>
      <pubDate>Tue, 27 Feb 2018 13:02:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/To-find-out-previous-date-out-of-one-set/m-p/440492#M13588</guid>
      <dc:creator>Sasforme</dc:creator>
      <dc:date>2018-02-27T13:02:15Z</dc:date>
    </item>
    <item>
      <title>Re: To find out previous date out of one set.</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/To-find-out-previous-date-out-of-one-set/m-p/440825#M13598</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt; - thank you so much it worked.</description>
      <pubDate>Wed, 28 Feb 2018 10:08:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/To-find-out-previous-date-out-of-one-set/m-p/440825#M13598</guid>
      <dc:creator>Sasforme</dc:creator>
      <dc:date>2018-02-28T10:08:19Z</dc:date>
    </item>
  </channel>
</rss>

