<?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: Calculations using dates on two separate data rows... in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Calculations-using-dates-on-two-separate-data-rows/m-p/236181#M55203</link>
    <description>&lt;P&gt;Just for the records:&lt;/P&gt;
&lt;P&gt;Solutions to this kind of problem using the RETAIN statement or (especially) the LAG&lt;EM&gt;n&lt;/EM&gt; functions, as have been suggested, are somewhat prone to unwanted carry-over effects from one BY group to another. Care has to be taken to avoid these. Neither RETAIN nor LAG/LAG&lt;EM&gt;n&lt;/EM&gt; respect BY groups by default.&lt;/P&gt;</description>
    <pubDate>Tue, 24 Nov 2015 12:17:50 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2015-11-24T12:17:50Z</dc:date>
    <item>
      <title>Calculations using dates on two separate data rows...</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculations-using-dates-on-two-separate-data-rows/m-p/235746#M55133</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm new to the forum, so I'm looking forward to not only getting some support from the community forums but also hoping to also provide some where possible.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can't seem to figure out the following (without considering transposing the data in some way). &amp;nbsp;I'm trying to do some calculations using dates but the dates I'm working with are on two separate records for the same individual. &amp;nbsp; I suspect there is some lag function, but this is an area that I am unfamiliar with.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is what the data looks like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID &amp;nbsp; &amp;nbsp; DATE1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; DATE2&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;2009-06-14 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2009-08-11&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;2009-10-29&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp;200912-31&lt;/P&gt;&lt;P&gt;&lt;SPAN style="line-height: 20px;"&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp;2009-09-05 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2009-11-10&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp;2009-10-09&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp;2010-05-23&lt;/P&gt;&lt;P&gt;and so on.....&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output I'm looking for is the following:&lt;/P&gt;&lt;P&gt;ID &amp;nbsp; &amp;nbsp; DATE1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; DATE2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DAYS&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; 2009-06-14 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2009-08-11 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 79 &amp;nbsp;&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; 2009-10-29&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; 2009-12-31&lt;/P&gt;&lt;P&gt;&lt;SPAN style="line-height: 20px;"&gt;2 &amp;nbsp; &amp;nbsp; 2009-09-05 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2009-11-10 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;194&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; 2009-10-09&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; 2010-05-23&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and so on.....&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, for the first person (ID #1) &amp;nbsp;(LAG)DATE1 - DATE2&amp;nbsp;= 20091029 - 20090811 = 79 days. &amp;nbsp; So, no further calculations are required.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For the 2nd person (ID #2) &amp;nbsp;(LAG)DATE1 - DATE2 = &amp;nbsp;20091009 - 20091110 = -32 days. &amp;nbsp;When a negative "DAYS" is encountered, I would like to be able to check the next record for that person to see if there is a non-negative count. &amp;nbsp;So, it would loop to the 3rd record whereby (LAG)DATE1 - DATE2 = 20100523 - 20091110 = 194 days.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For illustrative purposes, I've included the actual dates, but I did convert the dates into numeric values using&amp;nbsp;&amp;nbsp;NEWDATE1 = input (DATE1, yymmdd8.) to allow for the calculations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Essentially, I would like one record per person which tells me when did the next event (DATE1) occur after DATE2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help with this would be GREATLY appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for your time,&lt;/P&gt;&lt;P&gt;M. Anthony&lt;/P&gt;</description>
      <pubDate>Fri, 20 Nov 2015 18:10:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculations-using-dates-on-two-separate-data-rows/m-p/235746#M55133</guid>
      <dc:creator>crimdoc</dc:creator>
      <dc:date>2015-11-20T18:10:47Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations using dates on two separate data rows...</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculations-using-dates-on-two-separate-data-rows/m-p/235753#M55136</link>
      <description>Since you need to access the information on the next row, I think you need to re-sort your data in descending order on date1.&lt;BR /&gt;The you can use lag () or RETAIN (with BY and first./last.-logic). Which to use is a matter of taste, and for me I find RETAIN easier to follow. There a tons of lag () and RETAIN  examples on support.Sas.com.</description>
      <pubDate>Fri, 20 Nov 2015 18:35:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculations-using-dates-on-two-separate-data-rows/m-p/235753#M55136</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2015-11-20T18:35:17Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations using dates on two separate data rows...</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculations-using-dates-on-two-separate-data-rows/m-p/235756#M55137</link>
      <description>&lt;P&gt;I agree, you can't look down and then pull up.&amp;nbsp; If you want the presentation you have I think you need to sort:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;infile cards dsd;&lt;BR /&gt;informat d 5. date1 date2 yymmdd10.;&lt;BR /&gt;format d 5. date1 date2 yymmdd10.;&lt;BR /&gt;input D DATE1 DATE2;&lt;BR /&gt;cards;&lt;BR /&gt;1,2009-06-14,2009-08-11&lt;BR /&gt;1,2009-10-29,&lt;BR /&gt;1,2009-12-31,&lt;BR /&gt;2,2009-09-05,2009-11-10&lt;BR /&gt;2,2009-10-09,&lt;BR /&gt;2,2010-05-23,&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;proc sort data=have;by d descending date1;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;by d descending date1;&lt;BR /&gt;days2 = lag3(date1);&lt;BR /&gt;DAYS = lag(date1)-date2;&lt;BR /&gt;if days &amp;lt; 0 and not missing(days) then days = date2-days2;&lt;BR /&gt;drop days2;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc sort data=want;by d date1;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Nov 2015 19:09:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculations-using-dates-on-two-separate-data-rows/m-p/235756#M55137</guid>
      <dc:creator>Steelers_In_DC</dc:creator>
      <dc:date>2015-11-20T19:09:12Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations using dates on two separate data rows...</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculations-using-dates-on-two-separate-data-rows/m-p/235771#M55141</link>
      <description>&lt;P&gt;This will do it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
create table want as
select 
    a.*, 
    case 
        when date2 is missing then .
        else intck( "DAY", date2,
            (select min(date1) from have where id=a.id and date1 &amp;gt; a.date2) )
        end as days
from 
    have as a
order by id, date1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Nov 2015 20:08:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculations-using-dates-on-two-separate-data-rows/m-p/235771#M55141</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2015-11-20T20:08:14Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations using dates on two separate data rows...</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculations-using-dates-on-two-separate-data-rows/m-p/235787#M55148</link>
      <description>&lt;P&gt;If date1 of 3 or more later record in each ID is larger than date2, in such condition, you could try hash.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; if 0 then set have(keep=id date1);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; if _n_=1 then do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; declare hash h(dataset:'have(keep=id date1 rename=(date1=_date1))',multidata&amp;amp;colon;'y');&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; declare hiter hi('h');&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; h.definekey('id');&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; h.definedata('_date1');&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; h.definedone();&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; by id notsorted;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;if first.id then do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; rc=hi.setcur();&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do while (rc=0);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if _date1&amp;gt;date2 then do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; temp=_date1;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; goto day;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rc=hi.next();&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; day: day=temp-date2;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;drop temp _date1 rc;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Nov 2015 21:31:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculations-using-dates-on-two-separate-data-rows/m-p/235787#M55148</guid>
      <dc:creator>slchen</dc:creator>
      <dc:date>2015-11-20T21:31:16Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations using dates on two separate data rows...</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculations-using-dates-on-two-separate-data-rows/m-p/235808#M55155</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for all the quick replies and for the various suggestions. &amp;nbsp;For my current purposes, the solution by PGStats seemed to be relatively straightfoward and did the trick. &amp;nbsp; The other solutions are certain to come in hand in the future, so they are definitely worth adding to my syntax list.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again for the help.&lt;/P&gt;</description>
      <pubDate>Sat, 21 Nov 2015 01:05:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculations-using-dates-on-two-separate-data-rows/m-p/235808#M55155</guid>
      <dc:creator>crimdoc</dc:creator>
      <dc:date>2015-11-21T01:05:49Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations using dates on two separate data rows...</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculations-using-dates-on-two-separate-data-rows/m-p/236181#M55203</link>
      <description>&lt;P&gt;Just for the records:&lt;/P&gt;
&lt;P&gt;Solutions to this kind of problem using the RETAIN statement or (especially) the LAG&lt;EM&gt;n&lt;/EM&gt; functions, as have been suggested, are somewhat prone to unwanted carry-over effects from one BY group to another. Care has to be taken to avoid these. Neither RETAIN nor LAG/LAG&lt;EM&gt;n&lt;/EM&gt; respect BY groups by default.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Nov 2015 12:17:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculations-using-dates-on-two-separate-data-rows/m-p/236181#M55203</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2015-11-24T12:17:50Z</dc:date>
    </item>
  </channel>
</rss>

