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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 3 replies
  • 1178 views
  • 0 likes
  • 3 in conversation