Character Date to Numeric Date in SAS: What the Conversion Changes and When to Use It

How to convert a character date to a numeric SAS date with INPUT and an informat, what changes in storage versus display, and when to keep the original text.

Dates often arrive in SAS as text, for example from CSV files, spreadsheets or other systems' exports. A text value such as 03/15/2024 looks like a date, but SAS treats it as a string. You cannot reliably sort it chronologically, subtract one date from another, or apply date functions to it. Converting it to a numeric SAS date solves these problems. The conversion only preserves the meaning of the value if the right informat is used and the result is checked. This review covers what the conversion does, when it suits your purpose, and when keeping plain text is the better choice.

Is converting text dates to numeric dates the right move?

For most analytical work, yes. A numeric SAS date sorts in true chronological order, works with date functions such as INTCK, INTNX, YEAR and MONTH, and can be shown in any date format without changing the stored value. You get those benefits only when every source value follows a known, consistent pattern. When the source mixes day-first and month-first notation, a wrong informat can produce a valid but incorrect date with no error message. That risk matters more than any other part of the decision.

Plain text, or a converted column kept alongside the original text, is preferable in these situations:

  • The source contains partial or placeholder dates such as 2024-03, unknown or 00/00/2024, and you need to keep that information rather than turn it into missing values.
  • You must keep the exact original string for auditing, regulatory traceability or reconciliation with the source system.
  • The receiving system or file specification expects the date as text in a fixed layout.
  • You cannot yet tell whether the source uses month-first or day-first notation.

What the conversion actually changes

A SAS date is a number: the count of days since January 1, 1960. Converting a character date changes the stored data. A character variable holding a string becomes a numeric variable holding that day count. The INPUT function does this work. It reads the string according to an informat, which tells SAS how the text is laid out.

In SAS, the word format can mean two different things:

  • An informat, used with INPUT, controls how text is read and interpreted. It determines the stored value.
  • A format, attached with a FORMAT statement or used with PUT, controls only how the stored number is displayed. Changing it changes the display, never the stored value.

Applying a date format to a character variable does not convert it. Applying a format to a numeric date only changes how the day count appears. To change the stored value from text to a date, you need INPUT with an informat.

Matching the informat to the text

The informat must match the layout of the text exactly. Common pairings:

Text layout | Example | Informat
Day, month abbreviation, four-digit year | 15MAR2024 | DATE9.
Month first, US convention | 03/15/2024 | MMDDYY10.
Day first | 15/03/2024 | DDMMYY10.
ISO 8601 year-month-day | 2024-03-15 | YYMMDD10. or E8601DA10.
Mixed or uncertain layouts | various | ANYDTDTE. (see caveat below)

Locale matters for slash-separated dates. A string such as 03/04/2024 is March 4 when read with MMDDYY10. and April 3 when read with DDMMYY10. Both readings succeed, so the log will not warn you about the wrong choice. Before choosing, find out which convention the source system uses. Values with a day greater than 12 confirm the order, so look for those.

ANYDTDTE. accepts many layouts, which is convenient. For ambiguous values, though, it resolves the day and month order using the DATESTYLE system option, and that option's default depends on the session locale. The same code can therefore produce different dates in different environments. Use ANYDTDTE. only when you have checked how it resolves your data, and prefer a specific informat when the layout is consistent.

A basic conversion in a DATA step

A variable cannot change type within a data set. The usual pattern is to create a new numeric variable and keep the text version until the conversion has been checked:

data work.converted;
  set work.raw;
  order_date = input(strip(order_date_txt), ?? mmddyy10.);
  format order_date date9.;
run;

Each part of this step has a specific purpose. STRIP removes leading and trailing blanks that could cause a read to fail. The ?? modifier suppresses the invalid-data notes and the error flag for values that cannot be read; those values become missing. The FORMAT statement makes the new variable display as a readable date instead of a raw day count.

Because ?? hides problems in the log, check afterward for values that failed to convert:

proc sql;
  select order_date_txt, count(*) as n
  from work.converted
  where missing(order_date) and not missing(strip(order_date_txt))
  group by order_date_txt;
quit;

Any rows returned are text values that did not match the informat. Look at them before deciding whether to correct the source, change the informat, or accept them as missing.

If downstream code expects the original variable name, rename the text variable on input and reuse the name for the numeric result:

data work.converted;
  set work.raw(rename=(order_date=order_date_txt));
  order_date = input(strip(order_date_txt), ?? yymmdd10.);
  format order_date yymmdd10.;
run;

Once the failure check comes back clean, you can drop order_date_txt.

The same conversion works in PROC SQL:

proc sql;
  create table work.converted as
  select *, input(strip(order_date_txt), yymmdd10.) as order_date format=date9.
  from work.raw;
quit;

Two-digit years and date-time strings

When the text has only a two-digit year, SAS assigns the century using the YEARCUTOFF system option. The default differs between SAS releases, and sites can override it. Check the value in your session, or set it explicitly, before converting data with two-digit years.

Strings that include a time, such as 2024-03-15T14:30:00, hold a SAS datetime value, which counts seconds, not days. Read them with a datetime informat such as E8601DT. or ANYDTDTM. If you only need the date, apply DATEPART to the result. A datetime value read with a date informat, or treated as a date, gives wrong results.

Will the result stay readable after copying or sharing?

The stored value is a plain number. With no format attached, reports, the data viewer and some exports show the raw day count rather than a recognizable date. This is the most common usability problem after conversion. Attach a format permanently in the DATA step or SQL so that the date displays correctly wherever the data set is used.

When the data leaves SAS, the result depends on the export method and the destination. Some methods write the formatted value as text, and others pass the underlying number for the receiving application to interpret. The supplied material does not establish how a particular export method or destination application treats SAS dates. Test the specific path, for example to a CSV file, a spreadsheet or a database table, and check a few known dates on the receiving side instead of assuming that one successful transfer applies everywhere.

If people outside SAS will read the output, an unambiguous display format such as DATE9. or YYMMDD10. avoids the month-first versus day-first confusion that slash-separated formats can cause for an international audience. When a downstream system needs text in a fixed layout, create that text explicitly with PUT, for example put(order_date, yymmdd10.). Do not rely on default display behavior.

Gaps in this review

No release-specific documentation, test data or measured results were supplied for this review. The informats, options and behaviors described here are standard SAS features, but defaults such as YEARCUTOFF and the locale-dependent DATESTYLE setting vary by release and site configuration. Confirm them in the documentation for your SAS version and run the failure check above on your own data before relying on the converted dates.