<?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: Find difference between two dates.. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-difference-between-two-dates/m-p/109001#M22665</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;write out a new data set with what you are already doing well, just keeping the date1 variable... but also delete the first '.'&lt;/P&gt;&lt;P&gt;(you could add this to your data step)&lt;/P&gt;&lt;P&gt;keep date1;&lt;/P&gt;&lt;P&gt;if date1=. then delete;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;then, concatenate that data set to your original one so that the first obs of each will be put together, then the 2nd obs of each, etc.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data combined-data-set;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set original-set;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set new-data-set;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 27 Dec 2012 17:29:21 GMT</pubDate>
    <dc:creator>cau83</dc:creator>
    <dc:date>2012-12-27T17:29:21Z</dc:date>
    <item>
      <title>Find difference between two dates..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-difference-between-two-dates/m-p/108998#M22662</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;I have a data like below..&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data test;&lt;/P&gt;&lt;P&gt;input id name date anydtdte23.;&lt;/P&gt;&lt;P&gt;format date date9.;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;10 bb 2012-04-13 03:36:53.350&lt;/P&gt;&lt;P&gt;11 cc 2012-04-21 15:27:59.003&lt;/P&gt;&lt;P&gt;12 dd 2012-04-14 04:48:18.030&lt;/P&gt;&lt;P&gt;13 ee 2012-04-18 22:17:29.357&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I want to create a new flag, and&amp;nbsp; data should be like below(that means difference between 1st row minus second, second minus third so on.....)..&lt;/P&gt;&lt;P&gt;and if&amp;nbsp; i want to know, diff b/w months&amp;nbsp; or years ...how could i do...please provide the code for this.&lt;/P&gt;&lt;P&gt;flag&lt;/P&gt;&lt;P&gt;-7&lt;/P&gt;&lt;P&gt;7&lt;/P&gt;&lt;P&gt;-4&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;could any body help..that should be appreciated.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks &amp;amp; Regards,&lt;/P&gt;&lt;P&gt;Yaswanth &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 27 Dec 2012 09:45:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-difference-between-two-dates/m-p/108998#M22662</guid>
      <dc:creator>yaswanthj</dc:creator>
      <dc:date>2012-12-27T09:45:09Z</dc:date>
    </item>
    <item>
      <title>Re: Find difference between two dates..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-difference-between-two-dates/m-p/108999#M22663</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;What have you done so far? Could you please post some of your code and tell us where you've got stuck?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 27 Dec 2012 11:11:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-difference-between-two-dates/m-p/108999#M22663</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2012-12-27T11:11:02Z</dc:date>
    </item>
    <item>
      <title>Re: Find difference between two dates..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-difference-between-two-dates/m-p/109000#M22664</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Patrick,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for your quick reply..&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am working on below mentioned code..&lt;/P&gt;&lt;P&gt;data test;&lt;/P&gt;&lt;P&gt;input date anydtdte23.;&lt;/P&gt;&lt;P&gt;format date date9.;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;2012-04-13 03:36:53.350&lt;/P&gt;&lt;P&gt;2012-04-21 15:27:59.003&lt;/P&gt;&lt;P&gt;2012-04-14 04:48:18.030&lt;/P&gt;&lt;P&gt;2012-04-18 22:17:29.357&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;proc print data = test;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;data test1;&lt;/P&gt;&lt;P&gt;set test;&lt;/P&gt;&lt;P&gt;date1 = intck('days',date,lag(date));&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;proc print data = test1;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Output:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; Obs&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; date&amp;nbsp;&amp;nbsp;&amp;nbsp; date1&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 13APR2012&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 21APR2012&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -8&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14APR2012&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 18APR2012&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -4&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here i am getting the&amp;nbsp; difference between dates(date1) populating correctly..but the thing was i need the date1 difference should start from 1st row like below..&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;required output:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Obs&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; date&amp;nbsp;&amp;nbsp;&amp;nbsp; date1&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 13APR2012&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -8&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 21APR2012&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14APR2012&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 18APR2012&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Yaswanth&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 27 Dec 2012 11:25:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-difference-between-two-dates/m-p/109000#M22664</guid>
      <dc:creator>yaswanthj</dc:creator>
      <dc:date>2012-12-27T11:25:44Z</dc:date>
    </item>
    <item>
      <title>Re: Find difference between two dates..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-difference-between-two-dates/m-p/109001#M22665</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;write out a new data set with what you are already doing well, just keeping the date1 variable... but also delete the first '.'&lt;/P&gt;&lt;P&gt;(you could add this to your data step)&lt;/P&gt;&lt;P&gt;keep date1;&lt;/P&gt;&lt;P&gt;if date1=. then delete;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;then, concatenate that data set to your original one so that the first obs of each will be put together, then the 2nd obs of each, etc.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data combined-data-set;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set original-set;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set new-data-set;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 27 Dec 2012 17:29:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-difference-between-two-dates/m-p/109001#M22665</guid>
      <dc:creator>cau83</dc:creator>
      <dc:date>2012-12-27T17:29:21Z</dc:date>
    </item>
    <item>
      <title>Re: Find difference between two dates..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-difference-between-two-dates/m-p/109002#M22666</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;SAS data step doesn't allow you to read ahead to the next record, not within a single pass of the data anyway.&amp;nbsp; The LAG function is retaining the value from the &lt;SPAN style="text-decoration: underline;"&gt;previously&lt;/SPAN&gt; read record so that's why your initial attempt to put the date difference calculation was "offset" by one line (for your tastes anyway).&amp;nbsp;&amp;nbsp; Fortunately, you should be able to accomplish this type of staged calculation using a hash table approach in V9 of SAS.&amp;nbsp;&amp;nbsp; Try the following.....&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data test ;&lt;/P&gt;&lt;P&gt; input id name $2. date anydtdte23. ;&lt;/P&gt;&lt;P&gt; format date date9. ;&lt;/P&gt;&lt;P&gt; counter=_n_ ;&amp;nbsp;&amp;nbsp; /* create an observation counter from automatic PDV variable.&amp;nbsp; */&lt;/P&gt;&lt;P&gt; mykey=counter-1 ;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* create a trick field to apply as key to previous record in subsequent hash table step.&amp;nbsp; */&lt;/P&gt;&lt;P&gt; cards ;&lt;/P&gt;&lt;P&gt; ...... your data here .....&lt;/P&gt;&lt;P&gt; ;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;data test2(drop=rc&amp;nbsp; mykey&amp;nbsp; tempdt&amp;nbsp; counter ) ;&lt;/P&gt;&lt;P&gt; if 0 then set test(keep=mykey date&amp;nbsp; rename=(date=tempdt) ) ;&amp;nbsp;&amp;nbsp; /* rename date field to use it in calculation step later */&lt;/P&gt;&lt;P&gt; if _n_=1 then do;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* define the hash table definition once */&lt;/P&gt;&lt;P&gt;&amp;nbsp; declare&amp;nbsp; hash&amp;nbsp; aa(dataset:'work.test(keep=mykey date&amp;nbsp; where=(mykey&amp;gt;0)&amp;nbsp; rename=(date=tempdt) )'&amp;nbsp; ) ;&amp;nbsp;&amp;nbsp; /* throw out the first obs from original file */&lt;/P&gt;&lt;P&gt;&amp;nbsp; rc=aa.definekey('mykey') ;&amp;nbsp;&amp;nbsp; /*&amp;nbsp; key field on which to identify matching records */&lt;/P&gt;&lt;P&gt;&amp;nbsp; rc=aa.definedata('tempdt') ;&amp;nbsp;&amp;nbsp; /*&amp;nbsp; the data field(s) to introduce into the PDV when a match is found on the key value */&lt;/P&gt;&lt;P&gt;&amp;nbsp; rc=aa.definedone() ;&amp;nbsp;&amp;nbsp;&amp;nbsp; /*&amp;nbsp; end the definition of the hash object&amp;nbsp; */&lt;/P&gt;&lt;P&gt; end;&lt;/P&gt;&lt;P&gt; set&amp;nbsp; test(drop=mykey) ;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* read in all the original data but throw out the mykey field */&lt;/P&gt;&lt;P&gt; call missing(tempdt) ;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* initialize the temporary date field to missing for each pass of the main data, otherwise values would persist */&lt;/P&gt;&lt;P&gt; rc=aa.find( key: counter ) ;&amp;nbsp;&amp;nbsp; /* look in the hash object for a matching record, based on the value of the counter field */&lt;/P&gt;&lt;P&gt; diff=intck('day' , tempdt , date )&amp;nbsp; ;&lt;/P&gt;&lt;P&gt; format tempdt&amp;nbsp; date9. ;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;proc print ; run ;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 27 Dec 2012 17:31:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-difference-between-two-dates/m-p/109002#M22666</guid>
      <dc:creator>KoMartin66</dc:creator>
      <dc:date>2012-12-27T17:31:41Z</dc:date>
    </item>
    <item>
      <title>Re: Find difference between two dates..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-difference-between-two-dates/m-p/109003#M22667</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;KoMartin66 says: "SAS data step doesn't allow you to read ahead to the next record, not within a single pass of the data anyway."&lt;/P&gt;&lt;P&gt;Well there is no function for it, but that does not mean that you cannot do it!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is how I would solve the issue:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data test;&lt;/P&gt;&lt;P&gt;input date anydtdte23.;&lt;/P&gt;&lt;P&gt;format date date9.;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;2012-04-13 03:36:53.350&lt;/P&gt;&lt;P&gt;2012-04-21 15:27:59.003&lt;/P&gt;&lt;P&gt;2012-04-14 04:48:18.030&lt;/P&gt;&lt;P&gt;2012-04-18 22:17:29.357&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data test1;&lt;/P&gt;&lt;P&gt;set test;&lt;/P&gt;&lt;P&gt;set test(firstobs=2 rename=(date=nextdate)) test(drop = _all_) ;&lt;/P&gt;&lt;P&gt;difference = date - nextdate;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;proc print;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The key to the solution is to use 2 SET statements. The second one starts reading at the second observation and is therefore always one step ahead. It would therefore also hit "end of file" first. That is the reason to include the dataset also without the firstobs option, but with "drop=_all_".&lt;/P&gt;&lt;P&gt;You can calculate the difference like you did with the INTCK function, but hey, these are SAS dates, so numbers, thus you can simply&amp;nbsp; subtract them!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 27 Dec 2012 18:29:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-difference-between-two-dates/m-p/109003#M22667</guid>
      <dc:creator>ErikT</dc:creator>
      <dc:date>2012-12-27T18:29:38Z</dc:date>
    </item>
    <item>
      <title>Re: Find difference between two dates..</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-difference-between-two-dates/m-p/109004#M22668</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Below code uses the same approach like ErikT but is implemented with a MERGE statement instead of a combination of SET statements.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;input date anydtdte23.;&lt;BR /&gt;format date date9.;&lt;BR /&gt;cards;&lt;BR /&gt;2012-04-13 03:36:53.350&lt;BR /&gt;2012-04-21 15:27:59.003&lt;BR /&gt;2012-04-14 04:48:18.030&lt;BR /&gt;2012-04-18 22:17:29.357&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;&amp;nbsp; merge test test(firstobs=2 keep=date rename=(date=date1));&lt;BR /&gt;&amp;nbsp; Day_Diff=intck('days',date1,date);&lt;BR /&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 29 Dec 2012 01:13:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-difference-between-two-dates/m-p/109004#M22668</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2012-12-29T01:13:21Z</dc:date>
    </item>
  </channel>
</rss>

