Date Format in SAS: Displaying, Converting and Extracting Calendar Dates

Learn how SAS date formats work: how dates are stored, common formats like DATE9. and YYMMDD10., converting text to dates with informats, and checking results.

Working with calendar dates in SAS confuses many people because a date has two separate parts: the number SAS stores and the text it displays. A date format controls only the display. Once you understand that split, choosing the right format, turning raw text into real dates and checking your results all become straightforward.

What a SAS date format does to your data

SAS stores a calendar date as a plain number called a SAS date value: the count of days since January 1, 1960. That day is 0, January 2, 1960 is 1, and earlier dates are negative. Without a format, a date variable prints as that bare integer, which is correct but unreadable.

A format is a display rule applied to that number. It turns the day count into readable text such as 15MAR2024 or 2024-03-15 in reports, printed output and data views. It does not change the stored value, so you can switch formats freely without altering the date itself. This is the key safeguard: changing how a date looks never changes which day it represents.

An informat is the reverse rule. It reads text such as 03/15/2024 and converts it into a SAS date value. You need an informat whenever dates arrive as character strings, for example from a CSV file, a free-text field or an imported spreadsheet column stored as text.

Common SAS date formats

The table below shows widely used date formats and how each would display March 15, 2024. The displays are illustrative examples of each format's documented pattern; run them in your own session to confirm.

Format | Example display | Typical use
DATE9. | 15MAR2024 | Unambiguous default for logs and reports
MMDDYY10. | 03/15/2024 | US month-first style
MMDDYY8. | 03/15/24 | US style with two-digit year
DDMMYY10. | 15/03/2024 | Day-first style
YYMMDD10. | 2024-03-15 | Sortable year-first style
E8601DA10. | 2024-03-15 | ISO 8601 date
YYMMDDN8. | 20240315 | Compact text with no separators
MONYY7. | MAR2024 | Month-level summaries
WORDDATE. | March 15, 2024 | Readable report text
WEEKDATE. | Friday, March 15, 2024 | Report text including weekday
YEAR4. | 2024 | Year only

The number in a format name is its width, the count of characters available for display. If the width is too small for the output, SAS may abbreviate the text or show asterisks, so allow enough width for the full pattern.

Several formats accept a separator letter. In the YYMMDDx family, for example, x can be B (blank), C (colon), D (dash), N (no separator), P (period) or S (slash). So YYMMDDS10. displays 2024/03/15, and YYMMDDN8. displays 20240315.

Step 1: Apply a format to an existing date variable

Use a FORMAT statement in a DATA step to attach a format permanently to the variable in the output data set. The example below creates a date from a date literal, which is a quoted day-month-year string followed by the letter d.

data example;
  order_date = '15MAR2024'd;
  format order_date date9.;
run;

If you only want a different display in one report, put the FORMAT statement inside the procedure instead. That changes the output of that step without changing the format stored with the data set.

proc print data=example;
  format order_date worddate.;
run;

Step 2: Convert date text into a real SAS date

When a date is stored as character text, formats have no effect because there is no number to format. Convert it with the INPUT function and an informat that matches the text's layout exactly, then apply whichever display format you want.

data converted;
  raw_text = '2024-03-15';
  sas_date = input(raw_text, yymmdd10.);
  format sas_date mmddyy10.;
run;

The informat describes the incoming text and the format describes the output; they do not need to match. In this example the text is read as year-month-day and displayed month-first.

  • Match the informat to the source layout: YYMMDD10. for 2024-03-15, MMDDYY10. for 03/15/2024, DDMMYY10. for 15/03/2024, DATE9. for 15MAR2024.
  • If a column mixes layouts, the ANYDTDTE. informat tries to recognize several common patterns automatically.
  • With ANYDTDTE., a value like 03/04/2024 is ambiguous; the DATESTYLE= system option (for example MDY or DMY) decides which reading SAS uses.
  • Text that does not match the informat becomes a missing value, and SAS writes an invalid-argument note to the log.

If you expect some invalid entries and want to suppress the log notes, add the ?? modifier, as in input(raw_text, ?? yymmdd10.). The bad values still become missing, so count them afterward rather than assuming every row converted.

Step 3: Turn a SAS date back into text

Sometimes you need the formatted date as an actual character value, for example to build a file name or export a fixed text layout. The PUT function applies a format and returns a string.

data labels;
  set converted;
  date_text = put(sas_date, yymmddn8.);
run;

Unlike a FORMAT statement, PUT creates new character data. The result no longer behaves as a date: it will sort and compare as text, and date arithmetic will not work on it. Keep the numeric date variable if you still need calculations.

Extracting a date embedded in free text

If the date sits inside a longer string, locate it first and then convert the matched piece. The illustrative example below uses PRXMATCH with a regular expression for a year-month-day pattern; adjust the pattern and informat to your data.

data extracted;
  note = 'Invoice issued on 2024-03-15 by billing';
  pos = prxmatch('/\d{4}-\d{2}-\d{2}/', note);
  if pos > 0 then
    found_date = input(substr(note, pos, 10), ?? yymmdd10.);
  format found_date date9.;
  drop pos;
run;

The pattern only checks the shape of the text, not whether it is a real calendar day. A string like 2024-13-45 matches the pattern but fails the informat and becomes missing, which is the behavior you want: invalid dates are flagged rather than silently accepted.

Dates versus datetimes

A SAS datetime value counts seconds since midnight on January 1, 1960, not days. Applying a date format such as DATE9. to a datetime value produces nonsense or asterisks because the number is far too large to be a day count. Use a datetime format such as DATETIME20., or extract the date portion with DATEPART first.

data stamps;
  event_dt = '15MAR2024:09:30:00'dt;
  event_date = datepart(event_dt);
  format event_dt datetime20. event_date date9.;
run;

Step 4: Verify the result

  • Run PROC CONTENTS on the data set and confirm the date variable is numeric and carries the format you intended.
  • Print the variable with a numeric format such as BEST12. to see the underlying day count and confirm the value is unchanged.
  • Check the log for invalid-argument notes, or count missing values after conversion to catch text that failed to parse.
  • Spot-check a few rows against the original text, especially for day-first versus month-first sources where 03/04 could mean either March 4 or April 3.
proc contents data=converted;
run;

proc print data=converted;
  format sas_date best12.;
run;

Gaps to check in your own environment

This guide covers general, long-standing SAS date behavior. No version-specific evidence was supplied, so availability of particular formats, their default widths and locale-dependent output (such as month names in other languages through NLS formats) should be confirmed in the documentation for your SAS release before relying on them in production.