BookmarkSubscribeRSS Feed
PrudhviB
Obsidian | Level 7

Hi i have the following data in SAS EG,

 

I am trying to get the hour interval for the date-time stamp and also the productive time in that hour. And also any time exceeding that hour i want it to be added to the next hour. any suggestions?

 

ActivityStart timeEnd Timetime in hrsRequired Resulttotal hours between hours HH:MM:SSRequired result 
breakNov 1, 2019 5:18:13 PMNov 1, 2019 5:32:03 PM0.2305555565h-6h0:13:50 
workNov 1, 2019 5:46:45 PMNov 1, 2019 5:51:57 PM0.0866666675h-6h0:05:12 
workNov 1, 2019 6:00:16 PMNov 1, 2019 6:02:01 PM0.0291666676h-7h0:01:45 
lunchNov 1, 2019 7:33:16 PMNov 1, 2019 8:00:47 PM0.4586111117h-8h0:27:3147 seconds added to the next hour 
breakNov 1, 2019 9:46:04 PMNov 1, 2019 10:01:37 PM0.2591666679h-10h0:15:331 min 37 sec added to the next hour
breakNov 2, 2019 5:15:26 PMNov 2, 2019 5:31:28 PM0.2672222225h-6h0:16:02 
LunchNov 2, 2019 7:30:18 PMNov 2, 2019 8:01:00 PM0.5116666677h-8h0:30:421 min added to the next hour
breakNov 2, 2019 9:45:13 PMNov 2, 2019 10:00:15 PM0.2505555569h-10h0:15:0215 sec added to the next hour
breakNov 5, 2019 5:15:06 PMNov 5, 2019 5:30:18 PM0.2533333335h-6h0:15:12 
lunchNov 5, 2019 7:31:05 PMNov 5, 2019 8:01:11 PM0.5016666677h-8h0:30:061 min 11sec added to the next hour
BreakNov 5, 2019 9:45:07 PMNov 5, 2019 10:02:36 PM0.2913888899h-10h0:17:292 min 36sec added to the next hour

 

7 REPLIES 7
ballardw
Super User

Some details you need to explain:

 

Is that 5h-6h supposed to be a character or numeric variable?

Why are you ignoring AM/PM values? I would think that 5h is for 5AM not 5PM. You may need to explain exactly how you intend to use the value that you show as 5h-6h. Since the Datetime value SAS uses will be much more amenable to using Hour 17 instead of 5 for PM times how critical is it to have "5h"?

 

You should also show exactly how you mean that " any time exceeding that hour i want it to be added to the next hour" is to be implemented. Such as showing some start and end data.

 

Assuming that you actually have SAS datetime values then this is where I would start.

data example;
   starttime ="1Nov2019:19:33:16"dt;
   endtime   ="1Nov2019:20:00:47"dt; 
   starthour = hour(starttime);
   if hour(endtime)> hour(starttime) then carryover = intck('seconds',intnx('hour',starttime,1),endtime);
run;

If you don't have datetime values for starttime and endtime that is the first step.

PrudhviB
Obsidian | Level 7
Hi Ballardw, i used h as example, the hours would be 24 hr format.( for 5 am - 6 am its 5-6 and 5pm -6pm its 17-18).
any time exceeding that hour: may be i can explain with an example.

Example:

start time end time hour interval productive hr
1. 05:00 pm 06:15 pm 17-18 01:00:00
2. 06:15 pm 06:30 pm 18-19 00:30:00
(15+15 min from the previous hour)

If this make sense.
ballardw
Super User

@PrudhviB wrote:
Hi Ballardw, i used h as example, the hours would be 24 hr format.( for 5 am - 6 am its 5-6 and 5pm -6pm its 17-18).
any time exceeding that hour: may be i can explain with an example.

Example:

start time end time hour interval productive hr
1. 05:00 pm 06:15 pm 17-18 01:00:00
2. 06:15 pm 06:30 pm 18-19 00:30:00
(15+15 min from the previous hour)

If this make sense.

Please show with actual data the values that get changed with your intervals. As in actual input data and actual output data examples.

 

And why do you think that you need that hyphenated value? How does the hyphen add value? The only way to have a hyphen as part of the value is to have a character variable. Which will not sort correctly for most uses and if any calculations are contemplated the first thing you will need to do is create a numeric value.

You can create a custom format to show a desired value with the hyphen. You will need to decide what you want a time value that crosses midnight to look like.

PrudhviB
Obsidian | Level 7

@ballardw may be i am not being clear what i want. have attached the data below let me know if this work 

Input data:

this is employee logging into a in house system for adherence. 

some time the activity can be 2 hours long.

 

Actual StartActual End
Nov 1, 2019 5:02:49 PMNov 1, 2019 5:02:55 PM
Nov 1, 2019 5:41:18 PMNov 1, 2019 5:46:45 PM
Nov 1, 2019 5:46:45 PMNov 1, 2019 5:51:57 PM
Nov 1, 2019 5:51:57 PMNov 1, 2019 6:00:16 PM
Nov 1, 2019 6:00:16 PMNov 1, 2019 6:02:01 PM
Nov 1, 2019 6:02:01 PMNov 1, 2019 6:06:44 PM
Nov 1, 2019 6:31:08 PMNov 1, 2019 6:32:24 PM
Nov 1, 2019 6:35:57 PMNov 1, 2019 6:37:30 PM
Nov 1, 2019 6:41:48 PMNov 1, 2019 6:41:56 PM
Nov 1, 2019 6:42:42 PMNov 1, 2019 6:43:10 PM
Nov 1, 2019 6:44:01 PMNov 1, 2019 6:44:21 PM
Nov 1, 2019 6:47:52 PMNov 1, 2019 6:48:00 PM
Nov 1, 2019 8:26:36 PMNov 1, 2019 8:26:45 PM
Nov 1, 2019 8:37:05 PMNov 1, 2019 8:39:12 PM
Nov 1, 2019 8:43:21 PMNov 1, 2019 8:44:15 PM
Nov 1, 2019 9:35:37 PMNov 1, 2019 9:35:51 PM

 

required output : 

Shift time is employee's shift and i want this to be a divided into hour groups starting from 00 - 24 (which will include business hours for the company)

Productive time: this is the time difference or the actual time the employee is logged into the system from the input data.

 

DateShift-time (4pm-12am)Productive Time
1-Nov-1916:00-17:000 min 
1-Nov-1917:00-18:007 min
1-Nov-1918:00-19:0010 min
1-Nov-1919:00-20:000 min 
1-Nov-1920:00-21:003 min
1-Nov-1921:00-22:001 min
1-Nov-1922:00-23:000 min 
1-Nov-1923:00-00:000 min 

 

the reason why i want the hour interval to be 4-5 format is, i have another data set(files worked in each hour) that i will be joining to this which has hour interval as 4-5.

 

Hope this clarifies the doubt. please let me know if you have any questions. 

novinosrin
Tourmaline | Level 20

HI @PrudhviB  Welcome to SAS commnunities. In your table(dataset), do you have all variables in place other than Required Result 1 and Required Result2?

PrudhviB
Obsidian | Level 7
Thank you and yes, except the last three columns i have all other.
novinosrin
Tourmaline | Level 20

Hi @PrudhviB  Can we make this a little bit more interactive to make it interesting than me having to work like a typist plz?

 I have done the most part. Just run the below and see what you have. The last part is very simple math. Can you attempt? can you try?

 




data have;
infile cards  truncover;
input Activity $ @9 Start_time anydtdtm22. @33	End_Time anydtdtm22.  timeinhrs  ;
format  Start_time	End_Time datetime20.;
cards;
break   11/1/2019  5:18:13 PM	11/1/2019  5:32:03 PM	0.230555556
work	11/1/2019  5:46:45 PM	11/1/2019  5:51:57 PM	0.086666667
work	11/1/2019  6:00:16 PM	11/1/2019  6:02:01 PM	0.029166667
lunch	11/1/2019  7:33:16 PM	11/1/2019  8:00:47 PM	0.458611111
break	11/1/2019  9:46:04 PM	11/1/2019  10:01:37 PM	0.259166667
break	11/2/2019  5:15:26 PM	11/2/2019  5:31:28 PM	0.267222222
Lunch	11/2/2019  7:30:18 PM	11/2/2019  8:01:00 PM	0.511666667
break	11/2/2019  9:45:13 PM	11/2/2019  10:00:15 PM	0.250555556
break	11/5/2019  5:15:06 PM	11/5/2019  5:30:18 PM	0.253333333
lunch	11/5/2019  7:31:05 PM	11/5/2019  8:01:11 PM	0.501666667
Break	11/5/2019  9:45:07 PM	11/5/2019  10:02:36 PM	0.291388889
;


data want;
set have;
total_hours_between_hours=End_Time-Start_time;
hr=intck('hour',Start_time,End_Time);
if hr>0 then do;
/*additions*/
hr_addition=ifn(hr-1>0,hr-1,.);
min_addition=ifn(minute(End_Time)>0,minute(End_Time),.);
sec_addition=ifn(second(End_Time)>0,second(End_Time),.);
end;
drop hr;
format total_hours_between_hours time10.;
run;

If you understand the simple logic, it's easy to figure, nonetheless come back to us. Have a good one!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 876 views
  • 1 like
  • 3 in conversation