Data Step - Dates Part 02: DATETIME Values, DATEPART, TIMEPART, and Datetime Arithmetic
Overview
Part 01 (SAS_DATASTEP_L201) covered SAS date values: integers representing the number of days since 01-JAN-1960, date literals, informats, date component functions, and day arithmetic.
This lesson introduces the related but distinct concept of datetime values in SAS, which store both a date and a time of day in a single numeric value.
Datetime values are stored as the number of seconds elapsed since midnight on 01-JAN-1960. They are used when you need to record or calculate the exact time of an event, not just the date.
Typical use cases in clinical data: adverse event start and stop datetime, sample collection datetime, ECG or vital sign recording time.
SAS Datetime Values and Literals
A SAS datetime value is a numeric variable that counts seconds from midnight 01-JAN-1960 (not days).
The relationship between a date value and a datetime value is: datetime = date_value * 86400 + seconds_since_midnight.
Datetime literals in SAS code use single quotes with the suffix DT: '01JAN2024:08:30:00'dt
The format of the datetime literal is: 'DDMONYYYY:HH:MM:SS'dt — note the colon separating date and time portions.
To display a datetime value in a readable form, apply a DATETIME format (e.g., DATETIME20.) or a DATE9./TIME8. format after extracting the date or time portion.
SAS Log
DATETIME20. formats the numeric datetime value as a readable date-time string (e.g., 15MAR2024:09:30:00).
Inspect WORK.DATETIME_DEMO to confirm the three datetime values display correctly.
The raw numeric value of dt1 will be a very large integer (seconds since 01-JAN-1960) — the format makes it human-readable.
Always apply a DATETIME format when displaying datetime variables; without a format the raw second count is meaningless to a reader.
Dataset View
Reading Datetime Values with Informats
When reading datetime strings from raw data or character variables, use a datetime informat to convert them to SAS datetime values.
Common datetime informats: DATETIME20. for 'DDMONyyyy:HH:MM:SS' strings, YMDDTTM. for ISO 8601 format strings like '2024-03-15T09:30:00'.
The INPUT function with a datetime informat converts a character datetime string to a SAS numeric datetime value.
SAS Log
event_dtc is read as character ($20.) because the raw data is a text string.
INPUT with DATETIME20. converts the character string to a SAS datetime value stored in event_dt.
Confirm WORK.EVENTS_PARSED shows event_dt as a formatted datetime, not a raw number — if it shows a large integer the FORMAT statement may be missing.
Dataset View
DATEPART() and TIMEPART() Functions
A SAS datetime value encodes both date and time. Two functions extract each component separately.
DATEPART(datetime) returns the SAS date value (integer days) corresponding to the date portion of the datetime. Apply a DATE format to display it.
TIMEPART(datetime) returns the SAS time value (seconds since midnight) corresponding to the time portion of the datetime. Apply a TIME format to display it.
Once extracted, the date portion is a normal SAS date value and can be used with all date functions (YEAR, MONTH, DAY, INTCK, INTNX, etc.).
SAS Log
event_date is a SAS date value — apply DATE9. to display it as 15MAR2024.
event_time is a SAS time value (seconds since midnight) — apply TIME8. to display it as HH:MM:SS.
YEAR(event_date) extracts the 4-digit year; HOUR(event_time) extracts the hour of day as an integer.
Check WORK.EXTRACTED to confirm all four derived variables have the expected values for each event row.
Dataset View
Datetime Arithmetic
Because datetime values are stored in seconds, datetime arithmetic is performed in seconds — unlike date arithmetic which is in days.
To find elapsed time between two datetime values: elapsed_seconds = end_dt - start_dt.
Divide by 3600 to convert to hours, by 60 to convert to minutes, or by 86400 to convert to days.
This is essential for calculating duration of adverse events, time to first response, or time from dose to sample collection.
SAS Log
Both start_dtc and end_dtc are character strings — INPUT with DATETIME20. converts each to a SAS datetime value.
The arithmetic is straightforward because both values are in the same unit (seconds).
Subject 001: start to end is 5 hours 15 minutes = 18900 seconds = 5.25 hours.
Subject 003: 2 days 12 hours 15 minutes — verify elapsed_days is approximately 2.51.
Inspect WORK.ELAPSED and confirm the elapsed values match expected calculations.
Dataset View
Converting Between Date and Datetime
Sometimes you need to convert a SAS date value to a datetime value — for example to compare a date against a datetime in a merge or WHERE condition.
Multiply a date value by 86400 to produce the midnight datetime for that date: datetime_at_midnight = date_value * 86400.
Conversely, DATEPART() extracts the date from a datetime as shown above.
Never compare a date value directly to a datetime value without conversion — they use different scales (days vs seconds) and the comparison will produce incorrect results.
SAS Log
DT_MIDNIGHT is the datetime value for 15MAR2024 at 00:00:00.
Adding 12 * 3600 = 43200 seconds gives the datetime for 15MAR2024 at 12:00:00 (noon).
DATEPART(dt_noon) recovers the original date value — confirm it equals date_val.
Key Points
SAS datetime values are seconds since midnight 01-JAN-1960 — distinct from date values which are days since the same epoch.
Datetime literals use single quotes with the DT suffix: '15MAR2024:09:30:00'dt.
DATEPART() and TIMEPART() split a datetime into its date and time components respectively — apply DATE and TIME formats to display them correctly.
Datetime arithmetic is in seconds: divide by 3600 for hours, 86400 for days to convert to meaningful units.
To convert a SAS date to a datetime, multiply by 86400 to get the midnight datetime for that date.
Never compare a SAS date directly to a SAS datetime — convert one to the other's scale first.