SAS Convert Character to Date: Turning Text Dates into Numeric Date Values
How to convert a character date to a numeric SAS date with INPUT and informats, what actually changes in storage versus display, and how to handle regional date notation.
Converting a character date in SAS means reading a text string such as a year-month-day value and storing it as a numeric SAS date value. The standard tool is the INPUT function paired with a date informat that matches how the text is written. This review explains what the conversion changes, when it is the right choice, and where regional date notation can cause silent errors.
Is the conversion suitable for your use?
For most analytical work, yes. A numeric SAS date can be sorted chronologically, compared, subtracted to get intervals, and passed to date functions such as INTNX, INTCK, YEAR and MONTH. A character date can do none of this reliably; text sorting only matches chronological order when every value uses the same zero-padded year-first layout.
The decisive tradeoff is interpretation risk. The conversion depends on telling SAS the correct notation. If the source mixes layouts, or uses day-month and month-day order inconsistently, the result can be wrong without any error, or missing for some rows.
Keeping the original character value is preferable when the text is an identifier rather than a date, when values are partial or free-form (for example, only a year, or entries like "unknown"), or when the exact source text must be preserved for audit. In those cases, a common approach is to keep the character column and add a separate numeric date column.
What the conversion actually changes
SAS has only two variable types: character and numeric. A SAS date value is a number counting days from January 1, 1960, with earlier dates stored as negative numbers. Converting a character date therefore changes the stored data: a string becomes a number.
How that number looks on screen is a separate layer controlled by a format. Without a format, a converted date prints as a plain integer. Assigning a format such as DATE9., YYMMDD10. or MMDDYY10. changes only the display, not the stored value. Informats govern reading text into numbers; formats govern writing numbers out as text.
In a DATA step, the basic pattern looks like this:
data want;
set have;
date_num = input(date_char, yymmdd10.);
format date_num date9.;
run;
With this code, a character value of 2024-03-15 is read by the YYMMDD10. informat and stored as a numeric date. The DATE9. format then displays it as 15MAR2024. The underlying number is the same whichever format you attach.
The same conversion in PROC SQL:
proc sql;
create table want as
select *,
input(date_char, yymmdd10.) as date_num format=date9.
from have;
quit;
You cannot change a variable's type in place
Assigning a numeric result back to the same character variable does not make it numeric; SAS keeps the original type and converts the number back to text. To end up with a numeric variable under the original name, create a new variable, drop the old one, and rename:
data want;
set have;
date_num = input(date_char, yymmdd10.);
format date_num date9.;
drop date_char;
rename date_num = date_char;
run;
Choosing the informat for the notation
The informat must describe how the text is written, not how you want the result displayed. Common choices:
| Text layout | Informat |
| Year, month, day (for example ISO-style dates) | YYMMDDw. |
| Month, day, year (common US notation) | MMDDYYw. |
| Day, month, year (common in much of Europe and elsewhere) | DDMMYYw. |
| Day, three-letter month abbreviation, year | DATEw. |
| ISO 8601 date-time text | E8601DTw. |
| Mixed or uncertain layouts | ANYDTDTEw. |
The width w should cover the full length of the string, including separators. These informats generally accept common separators such as slashes, hyphens and periods, but test them against your actual data rather than assuming every variant is accepted.
Numbers that look like dates
Sometimes a date such as 20240315 is already stored as a numeric variable rather than text. That number is not a SAS date value. Convert it to text first with PUT, then read it with a date informat:
date_num = input(put(num_yyyymmdd, 8.), yymmdd8.);
format date_num date9.;
Date-time strings
If the text contains a time portion, reading it with a date-time informat produces a SAS datetime value, which counts seconds rather than days. Use DATEPART to extract the date when only the day matters:
dt_num = input(dt_char, e8601dt19.);
date_num = datepart(dt_num);
format dt_num datetime20. date_num date9.;
Regional date notation and ambiguity
Localization is where this conversion most often goes wrong. A value like 03/04/2024 is March 4 in month-day-year notation and April 3 in day-month-year notation. Both MMDDYY10. and DDMMYY10. will read it without complaint, so choosing the wrong informat produces valid-looking but incorrect dates.
ANYDTDTE. reads many layouts, which is convenient for messy input, but for ambiguous values it relies on the DATESTYLE system option to decide the order. DATESTYLE can be set to values such as MDY, DMY or YMD; its default follows the session locale. If data from different regions is mixed in one column, no single setting resolves it correctly, and the source or region of each record has to be used to pick the informat.
options datestyle=dmy;
data want;
set have;
date_num = input(date_char, anydtdte10.);
format date_num yymmdd10.;
run;
Two-digit years add another layer: SAS assigns the century using the YEARCUTOFF system option. Check its value in your session if the source uses two-digit years.
For month names written in a language other than English, SAS offers national-language (NL) date informats, such as NLDATE., that interpret text according to the session locale. Availability and behavior depend on your SAS release and locale settings, so verify against your environment's documentation before relying on them.
Handling invalid or missing values
When a string does not match the informat, INPUT returns a missing value and writes an invalid-argument note to the log, along with the ERROR flag being set. If invalid values are expected, the ?? modifier suppresses the notes and the error flag, but it also hides problems. A safer pattern is to count or flag rows where the source is non-blank but the result is missing:
date_num = input(date_char, ?? yymmdd10.);
if not missing(date_char) and missing(date_num) then bad_date = 1;
When different rows require different informats, INPUTN accepts the informat name as a character value, which lets you choose it per row based on another variable such as a region code.
Will the result stay usable when exported or shared?
Within SAS, the numeric date keeps its format when saved to a SAS data set. Outside SAS, the behavior depends on the export method and destination. If the format is not carried over, the receiving tool may show the raw day count instead of a recognizable date. Writing a CSV through an export that applies formats, or explicitly converting the value back to text with PUT and an unambiguous format, avoids that.
For sharing across regions, a year-first layout such as YYMMDD10. (producing values like 2024-03-15) avoids the month-day versus day-month ambiguity that MMDDYY. or DDMMYY. output would reintroduce. If a downstream system or reader needs plain text, produce that text deliberately rather than relying on the receiving tool to interpret a SAS-formatted value.
No tests against specific destinations, such as particular spreadsheet applications or databases, were available for this review. How those tools interpret exported SAS dates should be confirmed with a small sample before a full export.
Gaps in this review
- The code patterns reflect documented SAS behavior; they were not run against a specific SAS release for this article.
- Informat widths and accepted separators should be checked with your own data.
- Locale-dependent behavior, including DATESTYLE defaults and NL informats, varies with session settings and release.
- Export behavior for named destinations was not tested.
Bottom line
Converting a character date to a numeric SAS date is the right move whenever you need to sort, filter, calculate or summarize by date. Use INPUT with an informat that matches the source notation, attach a format for display, and create a new variable rather than overwriting the character one. The main risk is ambiguous regional notation; resolve it explicitly instead of trusting a flexible informat, and keep the original text when it carries meaning that a date value cannot represent.