Extract a date from a datetime value in SAS

How DATEPART pulls the calendar date out of a SAS datetime, how it differs from a display format like DTDATE9., and when a character date is the better output.

A SAS datetime value records both a calendar day and a time of day. Many tasks only need the day, such as grouping transactions by date, joining to a daily table or filtering on a reporting period. SAS offers several ways to get there. Some change the stored value. Others only change how the value is displayed. This review explains the difference and when each approach fits.

Short recommendation

Use the DATEPART function when you need a real date value for grouping, joining, comparing or date arithmetic. Its main benefit is that the result is a numeric SAS date, so it works with date functions, date formats and date literals. The tradeoff is that the new variable has no time of day. If you might need the time later, keep the original datetime variable as well, or capture the time with TIMEPART. If you only need the date to look right in a report, apply a datetime format such as DTDATE9. to the original variable instead, because that loses nothing. A plain-text date is the better choice only when the destination cannot interpret SAS numeric dates, and it comes at the cost of date arithmetic.

How SAS stores dates and datetimes

Both kinds of value are ordinary numbers. A SAS datetime counts seconds since midnight on January 1, 1960. A SAS date counts days since January 1, 1960. Because the units differ, a date format cannot read a datetime value correctly, and a datetime format cannot read a date value correctly. Most problems with extracting dates come from mixing these two units.

What DATEPART actually changes

DATEPART converts a count of seconds into a count of whole days. The stored number changes, and the time of day is dropped. The result is a SAS date value. Without a date format attached, it displays as a raw day count, so assign a format at the same time.

data work.events_dated;
  set work.events;
  event_date = datepart(event_dt);
  event_time = timepart(event_dt);   /* optional: keep the time separately */
  format event_date date9. event_time time8.;
run;

Based on documented behavior, an event_dt of 15MAR2024:14:30:00 gives an event_date that displays as 15MAR2024 and an event_time that displays as 14:30:00. This snippet is illustrative and was not run for this article.

Changing the display without changing the value

If the goal is only to show the date, a datetime format can display the date portion of the original value. DTDATE9. presents a datetime as a date such as 15MAR2024, but the stored value is still in seconds and still includes the time. This works for reports and listings. It does not help when you compare the variable with a date literal or join it to a table keyed by SAS dates, because the underlying number has not changed.

proc print data=work.events;
  var event_dt;
  format event_dt dtdate9.;
run;
Approach | Stored value after | Time of day kept? | Suited to
DATEPART(dt) with a date format | SAS date (days) | No | Grouping, joins, date arithmetic, date literals
DTDATE9. format on the datetime | Unchanged datetime (seconds) | Yes, hidden from display | Reports where only the shown date matters
INTNX('DTDAY', dt, 0) | Datetime at midnight (seconds) | Reset to midnight | Joins to other datetime values truncated to the day
PUT(DATEPART(dt), yymmdd10.) | Character text | No | Text files or systems that do not read SAS dates

Doing the same in PROC SQL

DATEPART works the same way in PROC SQL. Use the FORMAT= column option to attach a date format. The same function can also filter rows against a date literal.

proc sql;
  create table work.daily as
  select datepart(event_dt) as event_date format=date9.,
         count(*) as n_events
  from work.events
  where datepart(event_dt) between '01MAR2024'd and '31MAR2024'd
  group by calculated event_date;
quit;

When the datetime is actually text

DATEPART expects a numeric datetime. If your source column holds character text such as 2024-03-15T14:30:00, read it into a datetime first with INPUT and an informat that matches the pattern, then extract the date. A specific informat such as E8601DT. is more predictable than a guessing informat such as ANYDTDTM., which has to infer the layout and can misread ambiguous day and month orders.

data work.parsed;
  set work.raw;
  event_dt   = input(event_dt_text, e8601dt19.);
  event_date = datepart(event_dt);
  format event_dt datetime20. event_date yymmdd10.;
run;

Common mistakes

  • Putting a date format such as DATE9. on a datetime variable. The seconds count is far too large to be read as days, so the result is not a valid date.
  • Comparing a datetime variable directly to a date literal, as in where event_dt = '15MAR2024'd. Seconds and days do not match, so the filter does not select what you intended. Compare datepart(event_dt) instead, or use a datetime literal.
  • Assuming DATEPART adjusts for time zones. It only does arithmetic on the stored value. If the datetime was recorded in UTC, the extracted date is the UTC date.
  • Forgetting missing values. A missing datetime produces a missing date, so check row counts after grouping.

Will the result stay usable after export or sharing?

Inside SAS, a numeric date with a format attached keeps both its meaning and its usefulness. Outside SAS, the outcome depends on the destination and the export method. The raw value is a day count from 1960, and other software uses different starting points for its own date serial numbers. A date that arrives as an unformatted number can therefore look wrong or be misread. Whether a given export carries the format through was not tested for this article, so check a sample in the actual destination before relying on it.

Converting to character with PUT gives you plain text that any system or person can read, but the column stops being a date. If you choose text, YYMMDD10. (ISO 8601 style, such as 2024-03-15) is usually the safest pattern. It cannot be confused between month-first and day-first conventions, and it sorts correctly as text. MMDDYY10. is familiar to US readers but ambiguous to others. DATE9. uses uppercase English month abbreviations, which are readable but do not sort chronologically as text. How screen readers announce these patterns was not tested here.

Evidence gaps

This review relies on general, documented SAS date and time behavior. The code examples were not executed. Behavior was not verified across SAS versions, when queries are passed through to an external database, or in specific export destinations or assistive technologies. Confirm results with a small test in your own environment before using them in production.