<?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: Caculate actual work time. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Caculate-actual-work-time/m-p/249106#M46822</link>
    <description>&lt;P&gt;Do you have a fixed data structure in terms of records?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ie always these records in this order?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SHIFT&amp;gt;BREAK1&amp;gt;LUNCH&amp;gt;BREAK2&lt;/P&gt;</description>
    <pubDate>Wed, 10 Feb 2016 01:40:23 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-02-10T01:40:23Z</dc:date>
    <item>
      <title>Caculate actual work time.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Caculate-actual-work-time/m-p/249103#M46821</link>
      <description>&lt;P&gt;Hi, I provided below "Have" and "Want" data. "Have" is what I have and "Want" is what I want as the end result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically I have data for each ID on a date have shift times and different types of breaks. For example: If an agent works 8:00AM to 5:00PM and took lunch break from 12:00 to 1:00, my result data should show Actual time he worked, like, 8:00 to 11:59 and 1:01 to 5:00 PM.&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;Have &lt;BR /&gt; &lt;BR /&gt;Date &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ID &amp;nbsp; CODE &amp;nbsp; Start_time &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Stop_time &lt;BR /&gt;15-Feb 2151 Shift &amp;nbsp; &amp;nbsp; 15FEB2016:08:00:00 15FEB2016:17:00:00 &lt;BR /&gt;15-Feb 2151 Break1 15FEB2016:10:30:00 15FEB2016:10:45:00 &lt;BR /&gt;15-Feb 2151 Lunch &amp;nbsp;15FEB2016:12:00:00 15FEB2016:13:00:00 &lt;BR /&gt;15-Feb 2151 Break2 15FEB2016:15:15:00 15FEB2016:15:30:00 &lt;BR /&gt;15-Feb 3000 Shift &amp;nbsp; &amp;nbsp; 15FEB2016:09:00:00 15FEB2016:18:00:00 &lt;BR /&gt;15-Feb 3000 Break1 15FEB2016:10:30:00 15FEB2016:10:45:00 &lt;BR /&gt;15-Feb 3000 Lunch &amp;nbsp;15FEB2016:12:00:00 15FEB2016:13:00:00 &lt;BR /&gt;15-Feb 3000 Break2 15FEB2016:15:15:00 15FEB2016:15:30:00 &lt;BR /&gt; &lt;BR /&gt; &lt;BR /&gt; &lt;BR /&gt;Want &lt;BR /&gt; &lt;BR /&gt;Date &amp;nbsp; &amp;nbsp; &amp;nbsp; ID &amp;nbsp; CODE Start_time &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Stop_time &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Shift_Begin &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Shift_End&lt;BR /&gt;15-Feb 2151 Shift 15FEB2016:08:00:00 15FEB2016:10:29:29 15FEB2016:08:00:00 15FEB2016:17:00:00&lt;BR /&gt;15-Feb 2151 Shift 15FEB2016:10:45:00 15FEB2016:11:59:59 15FEB2016:08:00:00 15FEB2016:17:00:00&lt;BR /&gt;15-Feb 2151 Shift 15FEB2016:13:00:00 15FEB2016:15:14:59 15FEB2016:08:00:00 15FEB2016:17:00:00&lt;BR /&gt;15-Feb 2151 Shift 15FEB2016:15:30:00 15FEB2016:17:00:00 15FEB2016:08:00:00 15FEB2016:17:00:00&lt;BR /&gt;15-Feb 3000 Shift 15FEB2016:09:00:00 15FEB2016:10:29:29 15FEB2016:09:00:00 15FEB2016:18:00:00&lt;BR /&gt;15-Feb 3000 Shift 15FEB2016:10:45:00 15FEB2016:11:59:59 15FEB2016:09:00:00 15FEB2016:18:00:00&lt;BR /&gt;15-Feb 3000 Shift 15FEB2016:13:00:00 15FEB2016:15:14:59 15FEB2016:09:00:00 15FEB2016:18:00:00&lt;BR /&gt;15-Feb 3000 Shift 15FEB2016:15:30:00 15FEB2016:18:00:00 15FEB2016:09:00:00 15FEB2016:18:00:00&lt;/P&gt;</description>
      <pubDate>Wed, 10 Feb 2016 01:00:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Caculate-actual-work-time/m-p/249103#M46821</guid>
      <dc:creator>helloSAS</dc:creator>
      <dc:date>2016-02-10T01:00:44Z</dc:date>
    </item>
    <item>
      <title>Re: Caculate actual work time.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Caculate-actual-work-time/m-p/249106#M46822</link>
      <description>&lt;P&gt;Do you have a fixed data structure in terms of records?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ie always these records in this order?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SHIFT&amp;gt;BREAK1&amp;gt;LUNCH&amp;gt;BREAK2&lt;/P&gt;</description>
      <pubDate>Wed, 10 Feb 2016 01:40:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Caculate-actual-work-time/m-p/249106#M46822</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-02-10T01:40:23Z</dc:date>
    </item>
    <item>
      <title>Re: Caculate actual work time.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Caculate-actual-work-time/m-p/249109#M46823</link>
      <description>&lt;P&gt;No. This is just an example. There can be more breaks in different order.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Feb 2016 01:48:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Caculate-actual-work-time/m-p/249109#M46823</guid>
      <dc:creator>helloSAS</dc:creator>
      <dc:date>2016-02-10T01:48:34Z</dc:date>
    </item>
    <item>
      <title>Re: Caculate actual work time.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Caculate-actual-work-time/m-p/249110#M46824</link>
      <description>&lt;P&gt;Hmm...will the SHIFT always be first?&lt;/P&gt;</description>
      <pubDate>Wed, 10 Feb 2016 01:49:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Caculate-actual-work-time/m-p/249110#M46824</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-02-10T01:49:34Z</dc:date>
    </item>
    <item>
      <title>Re: Caculate actual work time.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Caculate-actual-work-time/m-p/249111#M46825</link>
      <description>&lt;P&gt;Also, what are you trying to do overall...there may be other/different ways. I'd do a SQL self join with the records after identifying SHIFT groups.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Feb 2016 01:51:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Caculate-actual-work-time/m-p/249111#M46825</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-02-10T01:51:04Z</dc:date>
    </item>
    <item>
      <title>Re: Caculate actual work time.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Caculate-actual-work-time/m-p/249112#M46826</link>
      <description>Yes. Shift is first. &lt;BR /&gt;</description>
      <pubDate>Wed, 10 Feb 2016 02:05:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Caculate-actual-work-time/m-p/249112#M46826</guid>
      <dc:creator>helloSAS</dc:creator>
      <dc:date>2016-02-10T02:05:17Z</dc:date>
    </item>
    <item>
      <title>Re: Caculate actual work time.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Caculate-actual-work-time/m-p/249251#M46867</link>
      <description>&lt;P&gt;Assuming you have the events ordered this should help you get started. Since you said your data doesn't always follow this format you'll likely have some modifications to account for things that this may not.&lt;/P&gt;
&lt;P&gt;It's not sleek or efficient, more like brute force....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
informat date yymmn6. start_time stop_time datetime21.;
format date yymon6. start_time stop_time datetime21.;
input Date        ID   CODE $   Start_time                       Stop_time ;
cards;
201502 2151 Shift     15FEB2016:08:00:00 15FEB2016:17:00:00 
201502 2151 Break1 15FEB2016:10:30:00 15FEB2016:10:45:00 
201502 2151 Lunch  15FEB2016:12:00:00 15FEB2016:13:00:00 
201502 2151 Break2 15FEB2016:15:15:00 15FEB2016:15:30:00 
201502 3000 Shift     15FEB2016:09:00:00 15FEB2016:18:00:00 
201502 3000 Break1 15FEB2016:10:30:00 15FEB2016:10:45:00 
201502 3000 Lunch  15FEB2016:12:00:00 15FEB2016:13:00:00 
201502 3000 Break2 15FEB2016:15:15:00 15FEB2016:15:30:00
;
run; 

data want;
set have;
by id;
length Event $8.;

retain shift_start shift_end Shift 0;


if first.id or Code='Shift' then do;
	shift_start=start_Time;
	shift_end=stop_time;
	Shift+1;
	output;
end;
else do;
	Event="Stop";
	Time=start_Time;;
	output;
	Event="Start";
	Time=stop_Time;
	output;
end;

format shift_start shift_end time datetime21.;
drop start_Time stop_time;
run;

data want;
set want;
by id shift;

lag_T = lag1(time);
if lag_T = . then lag_T=shift_start;

if event="Stop" then do;
work_start=lag_t;
work_end=time-1;
output;
end;
if last.shift then do;
work_start=time;
work_end=shift_end;
output;
end;

format work_start work_end lag_T datetime21.;

run;


proc print;
var date id event time shift_start shift_end lag_T shift work_start work_End;
run;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Feb 2016 16:56:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Caculate-actual-work-time/m-p/249251#M46867</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-02-10T16:56:21Z</dc:date>
    </item>
    <item>
      <title>Re: Caculate actual work time.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Caculate-actual-work-time/m-p/249266#M46871</link>
      <description>&lt;P&gt;Thank you for sharing the logic!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I did the below ang got the results I wanted&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data shift1; set shift;&lt;BR /&gt;format start_time1 stop_time1 time8.;&lt;BR /&gt;start_time1=timepart(start_time);&lt;BR /&gt;stop_time1=timepart(stop_time);&lt;BR /&gt;drop start_time stop_time;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc sort data=shift1; by date id; run;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data shift2;&lt;BR /&gt;set shift1;&lt;BR /&gt;by date id;&lt;BR /&gt;if first.id then output shift2;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table shift3 as&lt;BR /&gt;select a.*,b.start_time1 as shift_begin, b.stop_time1 as shift_end&lt;BR /&gt;from shift1 a left join shift2 b&lt;BR /&gt;on a.id=b.id and a.date=b.date;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;proc sort data=shift3; by date id start_time1 stop_time1; run;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data shift4;&lt;BR /&gt; recno=_n_+1;&lt;BR /&gt; set shift3 end=last;&lt;BR /&gt; by id;&lt;BR /&gt; if not last.id&lt;BR /&gt; then set shift3 (keep=start_time1 rename=(start_time1=next_stop_time1)) point=recno;&lt;BR /&gt; else next_stop_time1=shift_end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data shift5(drop=stop_time1 start_time1 rename=(next_start_time1=start_time next_stop_time1=stop_time));&lt;BR /&gt; set shift4;&lt;BR /&gt; format next_start_time1 time8.;&lt;BR /&gt; by id;&lt;BR /&gt; if first.id&lt;BR /&gt; then next_start_time1=shift_begin;&lt;BR /&gt; else next_start_time1=stop_time1;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Feb 2016 17:31:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Caculate-actual-work-time/m-p/249266#M46871</guid>
      <dc:creator>helloSAS</dc:creator>
      <dc:date>2016-02-10T17:31:22Z</dc:date>
    </item>
  </channel>
</rss>

