<?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 Time Duration in Different Locations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Time-Duration-in-Different-Locations/m-p/535074#M146885</link>
    <description>&lt;P&gt;Hi, I need help on how to get the duration between two timeslots:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In my HAVE dataset below, a mobile's log-in time is recorded. With that, I'd like to get the duration a mobile spent in "home" and in "work", whose values in my WANT dataset.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input mobile time :datetime21. scenario location $4.;&lt;BR /&gt;datalines;&lt;BR /&gt;600000001 13FEB2019:00:01:00 0 home&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;600000001&lt;/SPAN&gt; 13FEB2019:00:03:00 0 home&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;600000001&lt;/SPAN&gt; 13FEB2019:00:04:00 0 home&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;600000001&lt;/SPAN&gt; 13FEB2019:00:05:00 1 work&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;600000001&lt;/SPAN&gt; 13FEB2019:00:11:00 1 work&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;600000001 &lt;/SPAN&gt;&amp;nbsp;13FEB2019:00:13:00 0 home&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Resulting dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;input mobile duration :time. location $;&lt;BR /&gt;datalines;&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;600000001 &lt;/SPAN&gt;03:00:00 home&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;600000001 &lt;/SPAN&gt;09:00:00 work&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;set want;&lt;BR /&gt;hours=duration/60/60;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;</description>
    <pubDate>Wed, 13 Feb 2019 02:25:35 GMT</pubDate>
    <dc:creator>angeliquec</dc:creator>
    <dc:date>2019-02-13T02:25:35Z</dc:date>
    <item>
      <title>Time Duration in Different Locations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-Duration-in-Different-Locations/m-p/535074#M146885</link>
      <description>&lt;P&gt;Hi, I need help on how to get the duration between two timeslots:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In my HAVE dataset below, a mobile's log-in time is recorded. With that, I'd like to get the duration a mobile spent in "home" and in "work", whose values in my WANT dataset.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input mobile time :datetime21. scenario location $4.;&lt;BR /&gt;datalines;&lt;BR /&gt;600000001 13FEB2019:00:01:00 0 home&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;600000001&lt;/SPAN&gt; 13FEB2019:00:03:00 0 home&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;600000001&lt;/SPAN&gt; 13FEB2019:00:04:00 0 home&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;600000001&lt;/SPAN&gt; 13FEB2019:00:05:00 1 work&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;600000001&lt;/SPAN&gt; 13FEB2019:00:11:00 1 work&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;600000001 &lt;/SPAN&gt;&amp;nbsp;13FEB2019:00:13:00 0 home&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Resulting dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;input mobile duration :time. location $;&lt;BR /&gt;datalines;&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;600000001 &lt;/SPAN&gt;03:00:00 home&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;600000001 &lt;/SPAN&gt;09:00:00 work&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;set want;&lt;BR /&gt;hours=duration/60/60;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 13 Feb 2019 02:25:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-Duration-in-Different-Locations/m-p/535074#M146885</guid>
      <dc:creator>angeliquec</dc:creator>
      <dc:date>2019-02-13T02:25:35Z</dc:date>
    </item>
    <item>
      <title>Re: Time Duration in Different Locations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-Duration-in-Different-Locations/m-p/535090#M146898</link>
      <description>&lt;P&gt;You can use the NOTSORTED option to do this relatively easily.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Use NOTSORTED to tell SAS to use groups&lt;/P&gt;
&lt;P&gt;2. At first of each "group" retain start&lt;/P&gt;
&lt;P&gt;3. At last of each "group" calculate duration&lt;/P&gt;
&lt;P&gt;4. If a single record for location, exclude that record - not sure this makes sense, but you need to decide how to handle the last records.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by mobile scenario location notsorted;
retain start;

if first.location then start = time;
if last.location then duration = (time - start)/60/60;

*not sure this part is correct;
if first.location and last.location then delete;

if last.location then output;



run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/38234"&gt;@angeliquec&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi, I need help on how to get the duration between two timeslots:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In my HAVE dataset below, a mobile's log-in time is recorded. With that, I'd like to get the duration a mobile spent in "home" and in "work", whose values in my WANT dataset.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input mobile time :datetime21. scenario location $4.;&lt;BR /&gt;datalines;&lt;BR /&gt;600000001 13FEB2019:00:01:00 0 home&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;600000001&lt;/SPAN&gt; 13FEB2019:00:03:00 0 home&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;600000001&lt;/SPAN&gt; 13FEB2019:00:04:00 0 home&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;600000001&lt;/SPAN&gt; 13FEB2019:00:05:00 1 work&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;600000001&lt;/SPAN&gt; 13FEB2019:00:11:00 1 work&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;600000001 &lt;/SPAN&gt;&amp;nbsp;13FEB2019:00:13:00 0 home&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Resulting dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;input mobile duration :time. location $;&lt;BR /&gt;datalines;&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;600000001 &lt;/SPAN&gt;03:00:00 home&lt;BR /&gt;&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;600000001 &lt;/SPAN&gt;09:00:00 work&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;set want;&lt;BR /&gt;hours=duration/60/60;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Feb 2019 04:07:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-Duration-in-Different-Locations/m-p/535090#M146898</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-13T04:07:36Z</dc:date>
    </item>
    <item>
      <title>Re: Time Duration in Different Locations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-Duration-in-Different-Locations/m-p/535148#M146921</link>
      <description>&lt;P&gt;If you want to sum the locations even when they are apart, this would work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input MOBILE TIME :DATETIME21. SCENARIO LOCATION $4.;
datalines;
600000001 13FEB2019:00:01:00 0 home
600000001 13FEB2019:00:03:00 0 home
600000001 13FEB2019:00:04:00 0 home
600000001 13FEB2019:00:05:00 1 work
600000001 13FEB2019:00:11:00 1 work
600000001 13FEB2019:00:13:00 0 home
600000001 13FEB2019:00:15:00 0 home
600000001 13FEB2019:00:16:00 1 work
;
run;

data WANT;
  set HAVE (keep= TIME LOCATION);
  REALTIME = dif(TIME); 
  if LOCATION = lag(LOCATION) then do;
    HOMETIME+REALTIME*(LOCATION = 'home') ; 
    WORKTIME+REALTIME*(LOCATION = 'work') ; 
  end;
  else REALTIME = .; 
 HOURS = REALTIME/60;
run;

proc print data=want(Keep= HOURS LOCATION REALTIME HOMETIME WORKTIME);
run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&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="timesheet.PNG" style="width: 433px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/27111iCB8C1388A0CD927F/image-size/large?v=v2&amp;amp;px=999" role="button" title="timesheet.PNG" alt="timesheet.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Feb 2019 11:38:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-Duration-in-Different-Locations/m-p/535148#M146921</guid>
      <dc:creator>VRKiwi</dc:creator>
      <dc:date>2019-02-13T11:38:43Z</dc:date>
    </item>
  </channel>
</rss>

