How to Format Datetime Values in SAS

A practical guide to SAS datetime formats: how datetime values are stored, which formats to apply, how to convert them to text, and how to check the result.

A datetime format in SAS controls how a datetime value is displayed or written as text. It does not change the value itself. This guide explains what datetime formats do, which ones fit common needs, how to turn a datetime into a text string or a text string into a datetime, and how to check the result. The output samples below are illustrative examples based on documented format behavior. They have not been run for this article, so confirm them in your own SAS session.

What a datetime format does to your data

SAS stores a datetime value as a number: the count of seconds since midnight at the start of January 1, 1960. For example, an afternoon in March 2024 is stored as a number of roughly two billion seconds. Printed without a format, you see that raw number.

A format is a display rule applied to that number. Its input is a numeric datetime value, and its output is readable text such as 15MAR2024:14:30:45 or 2024-03-15T14:30:45. A format affects only how the value appears in reports, printed output, exported text, or strings created with the PUT function. The stored seconds stay exactly the same, so calculations and comparisons are not affected.

Several related terms come up throughout this guide:

  • Format: a rule that turns a stored value into displayed text, such as DATETIME20.
  • Informat: the reverse rule, which reads text and converts it into a stored value, such as E8601DT19.
  • Width: the number after the format name, which sets how many characters the output may use. In DATETIME20., the width is 20.
  • Datetime literal: a fixed datetime written directly in code, such as '15MAR2024:14:30:45'dt.

Check that you actually have a datetime value

SAS has three separate kinds of time-related numeric values, and each needs its own family of formats:

  • Date values count days since January 1, 1960. Use date formats such as DATE9. or YYMMDD10.
  • Time values count seconds since midnight. Use time formats such as TIME8.
  • Datetime values count seconds since midnight on January 1, 1960. Use datetime formats such as DATETIME20.

Using the wrong family is the most common cause of strange output. A date format applied to a datetime value usually prints asterisks or a meaningless date, because the number of seconds is far too large to be read as a count of days. A datetime format applied to a date value shows a time early on January 1, 1960, because a few thousand days read as seconds is only a few hours. If the variable is character rather than numeric, no numeric format will work until you convert it with an informat, as shown later.

Apply a format with the FORMAT statement

To control how a datetime variable is displayed, attach a format with a FORMAT statement. In a DATA step, the format is stored with the variable in the output data set. In a procedure such as PROC PRINT, it applies only to that step's output.

data work.events;
  event_dt = '15MAR2024:14:30:45'dt;
  format event_dt datetime20.;
run;

proc print data=work.events;
  format event_dt e8601dt19.;  /* display only, for this step */
run;

In this example, the data set keeps DATETIME20. as the variable's format, while PROC PRINT displays it in ISO 8601 style for that one report. In both cases, event_dt still holds the same number of seconds.

Common datetime formats and example output

The table shows commonly used formats applied to the example value '15MAR2024:14:30:45'dt. The results show expected behavior and are not verified output. Some formats right-align their text when the width is larger than the content, which adds leading spaces.

Format | Example output | Typical use
DATETIME18. | 15MAR2024:14:30:45 | Standard SAS datetime with a four-digit year
DATETIME20. | 15MAR2024:14:30:45 (with two leading spaces) | Common default width; pads the left side
DATETIME16. | 15MAR24:14:30:45 | Shorter output with a two-digit year
E8601DT19. | 2024-03-15T14:30:45 | ISO 8601 text for exchanging data with other systems
DTDATE9. | 15MAR2024 | Shows only the date part of a datetime
DTMONYY7. | MAR2024 | Month and year from a datetime
DTYEAR4. | 2024 | Year only from a datetime
TOD8. | 14:30:45 | Time of day from a datetime

Note the DT prefix in DTDATE9., DTMONYY7. and DTYEAR4. These are datetime versions of date formats. They read seconds, not days, so they can be applied directly to a datetime variable without extracting the date first.

Width matters. If the width is too small for the content, SAS may shorten the output, for example by dropping to a two-digit year or removing seconds. If even that does not fit, SAS prints asterisks. When output looks cut off or shows asterisks, increase the width before suspecting the data.

Convert a datetime to a text string with PUT

The FORMAT statement only changes display. To create a new character variable that contains the formatted text, for example to concatenate it with other text or write it to a file, use the PUT function:

data work.labels;
  set work.events;
  length dt_text $19;
  dt_text = put(event_dt, e8601dt19.);
  label_text = catx(' ', 'Logged at', dt_text);
run;

Here dt_text is a character variable, expected to hold 2024-03-15T14:30:45. Unlike the original numeric variable, it can no longer be used directly in date arithmetic. Keep the original datetime variable if you still need to calculate or sort by time. If you use a format with padding, such as DATETIME20., the string may include leading spaces. The STRIP function removes them, as in strip(put(event_dt, datetime20.)).

Convert text into a datetime with INPUT

If your datetimes arrive as character text, for example from a CSV file, convert them to numeric datetime values with the INPUT function and an informat that matches the text layout. The informat must match the exact pattern of the text, including separators such as the T between date and time.

data work.parsed;
  raw1 = '2024-03-15T14:30:45';
  raw2 = '15MAR2024:14:30:45';
  dt1 = input(raw1, e8601dt19.);
  dt2 = input(raw2, datetime18.);
  format dt1 dt2 datetime20.;
run;

When the incoming text does not follow a single fixed pattern, the ANYDTDTM. informat tries to recognize several common layouts. It is convenient, but ambiguous text such as 03/04/2024 can be read in more than one way. How SAS resolves the ambiguity depends on the DATESTYLE system option. Check a sample of the converted values before relying on it.

Extract the date or time part

Sometimes you need a true date or time value rather than just a display change, for example to join with a table keyed by date. The DATEPART function returns a date value (days), and TIMEPART returns a time value (seconds since midnight). The result belongs to a different family, so apply the matching format:

data work.split;
  set work.events;
  event_date = datepart(event_dt);
  event_time = timepart(event_dt);
  format event_date yymmdd10. event_time time8.;
run;

To go the other way and build a datetime from separate parts, the DHMS function combines a date value with hours, minutes and seconds, for example dhms(event_date, 14, 30, 45).

Build a custom layout with PROC FORMAT

If no built-in format produces the exact text you need, such as a date and time separated by a space instead of a T, define a picture format with datetime directives. Directives are codes that start with a percent sign. Adding a 0 after the percent sign, as in %0m, pads single-digit values with a leading zero.

proc format;
  picture isospace (default=19)
    low-high = '%Y-%0m-%0d %0H:%0M:%0S' (datatype=datetime);
run;

data _null_;
  dt = '15MAR2024:14:30:45'dt;
  put dt= isospace.;
run;

The expected log line is dt=2024-03-15 14:30:45. Use single quotes around the picture string. With double quotes, SAS may try to interpret the percent signs as macro calls. The datatype=datetime option is required. Without it, SAS treats the value as a plain number, and the directives do not work as intended.

Verify the result

Because formats change only appearance, a quick check confirms that you got the intended display without altering the data:

  • Print a few rows with and without the format, for example by adding format event_dt best12.; in a PROC PRINT step. The underlying number should be identical in both views.
  • Run PROC CONTENTS to confirm the variable is numeric and to see which format is attached.
  • Test a known value as a datetime literal and compare the formatted output with what you expect.
  • For converted text, compare a handful of raw strings with their parsed values, especially when using ANYDTDTM.
  • Look for asterisks or missing values (shown as a period), which signal a width problem, a format from the wrong family, or text the informat could not read.

Gaps and caveats

No source-specific evidence was supplied for this guide, so it relies on general, documented SAS behavior. Exact output can vary with your SAS version, system options and locale settings. Language-dependent formats in particular, such as the NL family, change with the LOCALE option. SAS datetime values also carry no time zone, so values gathered from systems in different zones need separate handling that is not covered here. Run the examples in your own environment before using them in production code.