<?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: Calculating date interval between between sorted records in a table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-date-interval-between-between-sorted-records-in-a/m-p/706789#M216988</link>
    <description>Since SAS stores dates as numbers, under the hood, you can subtract the two values and the DIF() function does exactly that. It takes the difference between the current and previous value of a column. &lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Thu, 17 Dec 2020 20:41:12 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2020-12-17T20:41:12Z</dc:date>
    <item>
      <title>Calculating date interval between between sorted records in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-date-interval-between-between-sorted-records-in-a/m-p/706742#M216943</link>
      <description>&lt;P&gt;I have a table that is sorted by a date column. In a new column (called interval), I want to be able to calculate the number of days between the date in the subsequent record. I feel like there has to be a simple way to do this, but I can't figure it out.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i.e. the first record would always be 0. Then it is 32 days from 6/17/2020 to 7/19/2020, and then 94 days from 7/19/2020 to 10/21/2020, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id latestdt :yymmdd10.;
format latestdt yymmddd10.;
datalines;
1 2020-06-17
2 2020-07-19
3 2020-10-21
4 2020-10-26
5 2020-11-05
6 2020-11-13
7 2020-11-13
; 
run;

data want;
input id latestdt :yymmdd10. interval;
format latestdt yymmddd10.;
datalines;
1 2020-06-17 0
2 2020-07-19 32
3 2020-10-21 94
4 2020-10-26 5
5 2020-11-05 10
6 2020-11-13 8
7 2020-11-13 0
; 
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 Dec 2020 18:42:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-date-interval-between-between-sorted-records-in-a/m-p/706742#M216943</guid>
      <dc:creator>csanfor2</dc:creator>
      <dc:date>2020-12-17T18:42:35Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating date interval between between sorted records in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-date-interval-between-between-sorted-records-in-a/m-p/706743#M216944</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    interval=latestdt-lag(latestdt);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 Dec 2020 18:46:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-date-interval-between-between-sorted-records-in-a/m-p/706743#M216944</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-12-17T18:46:19Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating date interval between between sorted records in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-date-interval-between-between-sorted-records-in-a/m-p/706744#M216945</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/78549"&gt;@csanfor2&lt;/a&gt;&amp;nbsp; DIF function is rather straight forward i would think-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input id latestdt :yymmdd10.;
format latestdt yymmddd10.;
datalines;
1 2020-06-17
2 2020-07-19
3 2020-10-21
4 2020-10-26
5 2020-11-05
6 2020-11-13
7 2020-11-13
; 
run;


data want;
 set have;
 interval=dif(latestdt);
 if not interval then interval=0;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 Dec 2020 18:46:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-date-interval-between-between-sorted-records-in-a/m-p/706744#M216945</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-12-17T18:46:58Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating date interval between between sorted records in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-date-interval-between-between-sorted-records-in-a/m-p/706789#M216988</link>
      <description>Since SAS stores dates as numbers, under the hood, you can subtract the two values and the DIF() function does exactly that. It takes the difference between the current and previous value of a column. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 17 Dec 2020 20:41:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-date-interval-between-between-sorted-records-in-a/m-p/706789#M216988</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-12-17T20:41:12Z</dc:date>
    </item>
  </channel>
</rss>

