BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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
3 REPLIES 3
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
deleted_user
Not applicable
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;
Peter_C
Rhodochrosite | Level 12
since it rounding try
hour= floor( mod( datetime_value, 3600 ) /3600 ) ;
(datetime values are a number of seconds)

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1925 views
  • 0 likes
  • 3 in conversation