Help using Base SAS procedures

Hour is next largest hour when extracting hour from datetime

Reply
N/A
Posts: 0

Hour is next largest hour when extracting hour from datetime

I have a datetime from which I would like to extract hour in a new column in the data set. For datetime values greater than 30minutes past the hour, the hour is reported as the next largest hour. Why is this happening and how do I fix it? Below is my code and the resulting output. NOTE: I also used time=timepart(timestamp); format time hour.; with the same result.

Tiffanie

data hours;
set periods;
hour=timepart(timestamp);
format hour hour. ;
run;

timestamp, hour
01MAY2010:15:45:00, 16
01MAY2010:16:16:00, 16
01MAY2010:16:38:00, 17
Super Contributor
Super Contributor
Posts: 3,174

Re: Hour is next largest hour when extracting hour from datetime

You have other options though you must consider the SAS DOC for HOUR format and the caveat note about "...rounding...".

If not suitable, consider using the HOUR function to extract the hour-portion and display it separately.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search argument, this topic / post:

hour format site:sas.com

DOC reference below:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000198051.htm

Excerpt below:
Details

SAS rounds hours based on the value of minutes in the SAS time value.

The HOUR format uses asterisks to format values that are outside the time range 0-24 hours, such as datetime values.
N/A
Posts: 0

Re: Hour is next largest hour when extracting hour from datetime

Thank you, Scott. I failed to read the literature before posting. Here's how I fixed it:

data hours;
set periods;
hour=hour(timestamp);
run;
Valued Guide
Posts: 2,174

Re: Hour is next largest hour when extracting hour from datetime

since it rounding try
hour= floor( mod( datetime_value, 3600 ) /3600 ) ;
(datetime values are a number of seconds)
Ask a Question
Discussion stats
  • 3 replies
  • 148 views
  • 0 likes
  • 3 in conversation