Extract clock time from a datetime value in SAS
How to pull the clock time, or just the hour, out of a SAS datetime value with TIMEPART and HOUR, which formats to use, and when a character time string is the better choice.
Extracting the time of day from a SAS datetime is a small transformation, but it is easy to get a result that looks right on screen and behaves wrongly later. This review explains what the common approaches actually change, which one suits which purpose, and where the result can lose meaning once it is exported, copied or shared.
Short recommendation
For most work, use the TIMEPART function and attach a time format such as TIME8. or TOD8. The result stays a numeric SAS time value. You can still sort it, compare it and calculate with it, and it displays as a readable clock time. If you only need the hour, the HOUR function returns it directly as a number from 0 to 23. The tradeoff is that the readable clock time exists only through the format. Anything that ignores SAS formats sees a plain count of seconds.
Convert to a character string, for example with PUT and TOD8., only when the time is headed for something that cannot interpret SAS formats. Examples are a text report, a label, a concatenated key, or a file read by another system. In that case the plain-text representation is the safer choice, because it reads the same everywhere.
What the transformation actually changes
SAS stores dates, times and datetimes as plain numbers. A datetime value is the number of seconds since midnight on January 1, 1960. A time value is the number of seconds since midnight, so it describes a clock time with no date attached. A date value is the number of days since January 1, 1960. Formats decide how these numbers are displayed, and they do not alter the stored number.
Each approach differs in whether it changes the stored value or only how the value is presented:
- TIMEPART(datetime): creates a new numeric value holding only the seconds since midnight. The date information is discarded from the result.
- HOUR(datetime), MINUTE(datetime), SECOND(datetime): create numeric components. These functions accept either time or datetime values.
- FORMAT statement on the original datetime (for example TOD8.): changes only the display. The stored value still includes the date. Sorting, grouping and comparisons still use the full datetime.
- PUT(TIMEPART(datetime), TOD8.): creates a character string such as 09:05:00. The value is no longer numeric, so time arithmetic would require converting it back with INPUT.
The format-only option causes the most confusion. A datetime column displayed with a time format looks like a column of clock times. However, two rows showing the same time on different days are still different values. Use TIMEPART when you need to group or compare by time of day across dates.
DATA step example
data times;
dt = '15MAR2024:14:30:45'dt;
t = timepart(dt); /* seconds since midnight */
hr = hour(dt); /* hour of day as a number */
t_char = put(t, tod8.);
format dt datetime20. t time8.;
run;
Based on documented behavior, t holds 52245, which is 14 × 3600 + 30 × 60 + 45, and it displays as 14:30:45 under TIME8. The variable hr holds 14, and t_char is the eight-character string 14:30:45. These values are worked out from the definitions above, not from a captured run, so check them in your own session.
PROC SQL equivalent
proc sql;
create table want as
select dt,
timepart(dt) as clock_time format=time8.,
hour(dt) as clock_hour
from have;
quit;
Choosing a time format
| Format | Typical display | Notes |
| TIME8. | 14:30:45 | Hours below 10 show a leading space instead of a zero. Designed for durations, so values above 24 hours are allowed. |
| TOD8. | 14:30:45 | Time of day with a leading zero, such as 09:05:00. Better for clock times and for character strings that must sort correctly. |
| HHMM5. | 14:31 | Hours and minutes only. Check whether seconds are rounded or dropped for your purpose before relying on it. |
| TIME12.3 | 14:30:45.000 | Shows fractional seconds when the source datetime carries them. |
The leading-space versus leading-zero difference matters mainly after conversion to character. A string beginning with a space sorts before a string beginning with a digit, so 9:05:00 and 10:00:00 may still sort as expected. Strings produced with TOD avoid the question entirely and are easier for other tools to parse.
When the source is not a numeric datetime
Two common starting points break the simple recipe.
- A character datetime such as 2024-03-15 14:30:45: convert it to a numeric datetime first with INPUT and a suitable informat. ANYDTDTM. or an informat matching the exact layout are typical choices. Then apply TIMEPART. Applying TIMEPART to a character variable does not work.
- A SAS date value: a date stores whole days only, so there is no clock time to extract. Passing a date to TIMEPART or HOUR treats the day count as seconds and produces a meaningless time. If your source is described as a date but you expect a time, confirm whether it is really a datetime that has been formatted to show only the date.
Will the result stay usable after export, copying or sharing?
A numeric time value carries its readability through the attached format. Inside SAS, in data sets and procedures that honor formats, it remains readable and fully usable. Outside SAS the outcome depends on whether formatted or raw values are written. This varies by export method and destination, and no tested evidence was available here to confirm specific tools. Check a sample of your own output before relying on it.
- Copying from a results window or report generally copies the displayed, formatted text. That text is a plain clock time with no link to the underlying value.
- If raw values reach a spreadsheet, the time appears as a count of seconds such as 52245, not as a clock time. Spreadsheets commonly represent time as a fraction of a day, so a direct numeric hand-off needs conversion either way.
- Character strings made with PUT and TOD8. read the same in any destination. They lose numeric behavior, though, so a receiving system must parse them before doing any time arithmetic.
- The extracted time has no date and no time zone. Two events at 14:30:45 on different days, or in different zones, become indistinguishable. Keep the original datetime alongside the extracted time if that context matters.
Accessibility and reading order are not affected by this transformation the way they are by visual text effects. The output is ordinary digits and colons, which assistive technology can read as normal text. The practical risk is ambiguity, not legibility. A bare time with no date or zone may be read correctly yet still be misunderstood.
Gaps in this review
Several areas were not verified by testing: how TIMEPART behaves on datetimes before 1960, how HHMM rounds, and whether specific export procedures write formatted or raw values. Check these against the SAS documentation for your release, or with a small test on your own data, before building on them.