Desktop productivity for business analysts and programmers

hour function not working when time > 24 hrs

Reply
Contributor kdp
Contributor
Posts: 31

hour function not working when time > 24 hrs

I have data that is coming in with timestamps with more than 24 hours...e.g. 26:32:17.

When using the hour function on this timestamp, it returns 2 and not 26.

How can I make it recognize 26 rather than 2? I tried converting the time-stamp into a character value and then parsing out the hour. However, that didn't work either.

Thanks,
kdp
Trusted Advisor
Posts: 1,056

Re: hour function not working when time > 24 hrs

I'm guessing a bit about how you're turning your timestamp into a datetime value, but I think your problem is the difference between a time and a duration. Inputting 26:32:17 is most likely creating a datetime of 2 AM, January 2 1960. So the HOUR function is correctly returning 2.

The INTCK function returns information about an interval. If you ask for the number of hours in the interval between 12 AM January 1 1960 (datetime value 0) and your timestamp, you should get the correct 26.

I've put together a small sample program to demonstrate the difference.

Program
----------

data timetest;
chartime = "26:32:17";
timeval = input(chartime, time8.);
file log;
As_datetime = put(timeval, datetime.); put As_datetime =;
As_hour = hour(timeval); put As_hour =;
As_interval = intck("HOUR",0,timeval); put As_interval =;
run;

Log
---

As_datetime=02JAN60:02:32:17
As_hour=2
As_interval=26
Super Contributor
Super Contributor
Posts: 3,174

Re: hour function not working when time > 24 hrs

Another option is to run the data-string as formatted value then use INPUT to convert back to number.

Scott Barry
SBBWorks, Inc.


27 data _null_;
28 hr = input(put('26:00:00't,hour.),best.);
29 put hr= ;
30 run;

hr=26
NOTE: DATA statement used ...
Contributor kdp
Contributor
Posts: 31

Re: hour function not working when time > 24 hrs

Thanks guys! I knew someone would have an answer.
Ask a Question
Discussion stats
  • 3 replies
  • 144 views
  • 0 likes
  • 3 in conversation