<?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: Most recent two dates and difference in days across rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/409697#M100110</link>
    <description>Thank you again!</description>
    <pubDate>Thu, 02 Nov 2017 03:15:46 GMT</pubDate>
    <dc:creator>m_o</dc:creator>
    <dc:date>2017-11-02T03:15:46Z</dc:date>
    <item>
      <title>Most recent two dates and difference in days across rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/408323#M99634</link>
      <description>&lt;P&gt;I have a large dataset with rows of IDs and dates as below&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; testdate&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 11/1/16&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 6/4/17&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 8/1/17&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 6/4/16&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 7/5/16&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 10/1/16&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the actual dataset, there are a lot more rows per person than I've shown above. I would like to pick the most recent two test dates for each person, and then create a new variable with the difference in days between those two dates.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would really appreciate anyone's help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;KM&lt;/P&gt;</description>
      <pubDate>Sun, 29 Oct 2017 07:18:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/408323#M99634</guid>
      <dc:creator>m_o</dc:creator>
      <dc:date>2017-10-29T07:18:22Z</dc:date>
    </item>
    <item>
      <title>Re: Most recent two dates and difference in days across rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/408324#M99635</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 have;
input ID$  testdate:ddmmyy10.;
format testdate ddmmyy10.;
datalines;
1  11/01/16
1  06/04/17
1  08/01/17
2  06/04/16
2  07/05/16
2  10/01/16
;

proc sort data=have;
   by ID testdate;
run;

data want;
   set have;
   by ID;
   days_between=intck('day',lag1(testdate), testdate);
   if last.id;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 29 Oct 2017 08:16:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/408324#M99635</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-10-29T08:16:37Z</dc:date>
    </item>
    <item>
      <title>Re: Most recent two dates and difference in days across rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/408325#M99636</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/133150"&gt;@m_o&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Or as a variant to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;s code&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   by ID;
   days_between=dif(testdate);
   if last.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 29 Oct 2017 08:30:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/408325#M99636</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-10-29T08:30:38Z</dc:date>
    </item>
    <item>
      <title>Re: Most recent two dates and difference in days across rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/408411#M99698</link>
      <description>&lt;P&gt;Thank you very much for your response!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I ran the code. It seems to be working for ID 2 but not for person ID 1.&lt;/P&gt;&lt;P&gt;The difference between 8/1/17 and 6/04/17 is 58 days but the code seems to be giving 88 days.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was wondering if you are getting the same thing and if so how would one correct it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, on&amp;nbsp;a related topic, I was wondering if you could advise me on how to first subset the dataset&amp;nbsp;so that the new dataset contains only the two most recent test dates per person.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much!&lt;/P&gt;</description>
      <pubDate>Sun, 29 Oct 2017 21:01:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/408411#M99698</guid>
      <dc:creator>m_o</dc:creator>
      <dc:date>2017-10-29T21:01:02Z</dc:date>
    </item>
    <item>
      <title>Re: Most recent two dates and difference in days across rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/408413#M99700</link>
      <description>Thank you so much for your reply! I ran your code as well and get the same issue. If you could see my reply to draycut, and advise me on how to address those issues, I would really appreciate it! Thank you!</description>
      <pubDate>Sun, 29 Oct 2017 21:03:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/408413#M99700</guid>
      <dc:creator>m_o</dc:creator>
      <dc:date>2017-10-29T21:03:52Z</dc:date>
    </item>
    <item>
      <title>Re: Most recent two dates and difference in days across rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/408425#M99705</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/133150"&gt;@m_o&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;"The difference between 8/1/17 and 6/04/17 is 58 days but the code seems to be giving 88 days."&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;The code does the right thing. The explanation is that the dates are read into SAS using DDMMYYY and NOT US style MMDDYY. Change the informat in the sample code for reading the raw data and you'll get the expected result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;"so that the new dataset contains only the two most recent test dates per person."&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Please provide a sample how the desired result should look like especially in regards of the values for column &lt;EM&gt;days_between&lt;/EM&gt;.&lt;/P&gt;</description>
      <pubDate>Sun, 29 Oct 2017 23:03:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/408425#M99705</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-10-29T23:03:11Z</dc:date>
    </item>
    <item>
      <title>Re: Most recent two dates and difference in days across rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/408435#M99713</link>
      <description>&lt;P&gt;I see. Thank you! I would like the final dataset to look as shown below. If can first subset the dataset so that it only includes the two most recent test dates, then the next step of calculating days_between&amp;nbsp;might use less memory, as my dataset is large&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID testdate days_between&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 6/4/17&amp;nbsp; 88&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 8/1/17&amp;nbsp; 88&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 7/5/16&amp;nbsp; 31&lt;/P&gt;&lt;P&gt;2 10/1/16&amp;nbsp; 31&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was wondering if you could advise me on an additional data step that would allow me to subset the dataset as I have described.&lt;/P&gt;&lt;P&gt;Thank you very much for your help!&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2017 16:07:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/408435#M99713</guid>
      <dc:creator>m_o</dc:creator>
      <dc:date>2017-11-01T16:07:12Z</dc:date>
    </item>
    <item>
      <title>Re: Most recent two dates and difference in days across rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/408654#M99790</link>
      <description>&lt;PRE&gt;
Assuming I understood what you mean.


data have;
input ID$  testdate:mmddyy10.;
format testdate mmddyy10.;
datalines;
1  11/01/16
1  06/04/17
1  08/01/17
2  06/04/16
2  07/05/16
2  10/01/16
;

proc sort data=have;
   by ID descending testdate ;
run;
data want;
 set have;
 by id;
 if first.id then n=0;
 n+1;
 if n&amp;lt;3 then output;
 drop n;
run;


&lt;/PRE&gt;</description>
      <pubDate>Mon, 30 Oct 2017 13:37:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/408654#M99790</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-10-30T13:37:55Z</dc:date>
    </item>
    <item>
      <title>Re: Most recent two dates and difference in days across rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/409527#M100051</link>
      <description>That works great. Thank you so much!!</description>
      <pubDate>Wed, 01 Nov 2017 16:05:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/409527#M100051</guid>
      <dc:creator>m_o</dc:creator>
      <dc:date>2017-11-01T16:05:59Z</dc:date>
    </item>
    <item>
      <title>Re: Most recent two dates and difference in days across rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/409697#M100110</link>
      <description>Thank you again!</description>
      <pubDate>Thu, 02 Nov 2017 03:15:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/409697#M100110</guid>
      <dc:creator>m_o</dc:creator>
      <dc:date>2017-11-02T03:15:46Z</dc:date>
    </item>
    <item>
      <title>Re: Most recent two dates and difference in days across rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/409698#M100111</link>
      <description>Thank you so much! I have accepted the first responder's solution since he/she responded to the original question, but your solution works great as well! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Thanks!</description>
      <pubDate>Thu, 02 Nov 2017 03:16:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/409698#M100111</guid>
      <dc:creator>m_o</dc:creator>
      <dc:date>2017-11-02T03:16:49Z</dc:date>
    </item>
    <item>
      <title>Re: Most recent two dates and difference in days across rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/409700#M100113</link>
      <description>Thank you again!!</description>
      <pubDate>Thu, 02 Nov 2017 03:17:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Most-recent-two-dates-and-difference-in-days-across-rows/m-p/409700#M100113</guid>
      <dc:creator>m_o</dc:creator>
      <dc:date>2017-11-02T03:17:14Z</dc:date>
    </item>
  </channel>
</rss>

