<?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: sort and calculate time difference in seconds by comparing to the line below in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sort-and-calculate-time-difference-in-seconds-by-comparing-to/m-p/860776#M340029</link>
    <description>&lt;P&gt;hi ballardw,&lt;/P&gt;
&lt;P&gt;the code works but the numbers generated for duration are negative (note that seriestimestamp is the time_start)&lt;/P&gt;
&lt;P&gt;thanks&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="rykwong_0-1677273663075.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80834iA9A3C58E5F2CECFF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="rykwong_0-1677273663075.png" alt="rykwong_0-1677273663075.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 24 Feb 2023 21:21:44 GMT</pubDate>
    <dc:creator>rykwong</dc:creator>
    <dc:date>2023-02-24T21:21:44Z</dc:date>
    <item>
      <title>sort and calculate time difference in seconds by comparing to the line below</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sort-and-calculate-time-difference-in-seconds-by-comparing-to/m-p/860340#M339878</link>
      <description>&lt;P&gt;hi SAS community&lt;/P&gt;
&lt;P&gt;i would like to have a code to calculate the values in "duration".&amp;nbsp; THe dataset contains multiple patient MRI studies (Patient name).&amp;nbsp; Each MRI study contains a variable number of sequences (e.g a,b,c for patient 1, whereas a,b,c,d for patient 3).&amp;nbsp; Time start is the time that each sequence starts.&amp;nbsp; Duration is what I would like to calculate: which is calculated by subtracting the time start from the line below (after sorting the dataset in this order).&amp;nbsp; Obviously the data will have be to grouped by the MRI studies, so the last sequence of any given study does not have duration calculated (thus left blank) as we do not have the end time of the study.&amp;nbsp; Many thanks for your help in advance.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="rykwong_0-1677119015629.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80742iBEB049F7CEC9B21A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="rykwong_0-1677119015629.png" alt="rykwong_0-1677119015629.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Feb 2023 02:24:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sort-and-calculate-time-difference-in-seconds-by-comparing-to/m-p/860340#M339878</guid>
      <dc:creator>rykwong</dc:creator>
      <dc:date>2023-02-23T02:24:25Z</dc:date>
    </item>
    <item>
      <title>Re: sort and calculate time difference in seconds by comparing to the line below</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sort-and-calculate-time-difference-in-seconds-by-comparing-to/m-p/860343#M339881</link>
      <description>&lt;P&gt;Since SAS provides functions to look at previous record values I would sort and run things in reverse order&amp;nbsp; then sort back.&lt;/P&gt;
&lt;PRE&gt;proc sort data=have;
   by patientname descending time_start;
run;

data want;
   set have;
   duration=dif(time_start);
   by patientname;
   if first.patientname then duration=.;
run;

proc sort data=want;
   by patientname time_start;
run;&lt;/PRE&gt;
&lt;P&gt;The Dif function is difference of the current record from the previous. Using By processing so make sure that the first dif does not use the value from the previous patient.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Warning: Do you have any of these sequences that cross midnight? Your actual time, if time of day, may be an issue. Next, is if your times are using a 12 hour clock and not a 24 hour clock then crossing noon might an issue since you have not actually provided any values in that time range.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Feb 2023 04:05:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sort-and-calculate-time-difference-in-seconds-by-comparing-to/m-p/860343#M339881</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-02-23T04:05:08Z</dc:date>
    </item>
    <item>
      <title>Re: sort and calculate time difference in seconds by comparing to the line below</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sort-and-calculate-time-difference-in-seconds-by-comparing-to/m-p/860345#M339882</link>
      <description>Great solution!  Thanks so much</description>
      <pubDate>Thu, 23 Feb 2023 02:56:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sort-and-calculate-time-difference-in-seconds-by-comparing-to/m-p/860345#M339882</guid>
      <dc:creator>rykwong</dc:creator>
      <dc:date>2023-02-23T02:56:39Z</dc:date>
    </item>
    <item>
      <title>Re: sort and calculate time difference in seconds by comparing to the line below</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sort-and-calculate-time-difference-in-seconds-by-comparing-to/m-p/860761#M340022</link>
      <description>no data that can cross midnight &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;but the data is in 12 hour clock and may cross noontime so I guess i must convert the times to 24 h clock as a solution, right?&lt;BR /&gt;</description>
      <pubDate>Fri, 24 Feb 2023 20:10:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sort-and-calculate-time-difference-in-seconds-by-comparing-to/m-p/860761#M340022</guid>
      <dc:creator>rykwong</dc:creator>
      <dc:date>2023-02-24T20:10:30Z</dc:date>
    </item>
    <item>
      <title>Re: sort and calculate time difference in seconds by comparing to the line below</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sort-and-calculate-time-difference-in-seconds-by-comparing-to/m-p/860776#M340029</link>
      <description>&lt;P&gt;hi ballardw,&lt;/P&gt;
&lt;P&gt;the code works but the numbers generated for duration are negative (note that seriestimestamp is the time_start)&lt;/P&gt;
&lt;P&gt;thanks&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="rykwong_0-1677273663075.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80834iA9A3C58E5F2CECFF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="rykwong_0-1677273663075.png" alt="rykwong_0-1677273663075.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 21:21:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sort-and-calculate-time-difference-in-seconds-by-comparing-to/m-p/860776#M340029</guid>
      <dc:creator>rykwong</dc:creator>
      <dc:date>2023-02-24T21:21:44Z</dc:date>
    </item>
  </channel>
</rss>

