SAS: Convert a Date to Character, Including YYYYMM Strings

How to turn a SAS date into a character value with PUT and date formats, how to build YYYYMM strings, and when to keep the date numeric instead.

In SAS, a date is a number, not text. Turning it into a character string is a common step before building file names, joining keys, exporting reports or writing labels such as 202310. This review covers what the conversion changes, how to produce a YYYYMM string, and when you should keep the value numeric. The examples follow documented SAS function and format behavior. They were not run for this article, so test them in your own SAS session and locale before relying on them.

Is converting the date to character the right choice?

Convert to character when the destination needs fixed text. Examples include a key column, a file or table name, a concatenated label, or an export field that must arrive exactly as written. The benefit is that the string reads the same everywhere, with no dependence on a format being attached.

The tradeoff is that you lose date arithmetic. A character value cannot be compared, sorted chronologically (unless its layout allows it), shifted with INTNX, or measured with INTCK until you convert it back with INPUT. If you only want the date to look different in output, do not convert it. Attach a format to the numeric variable instead.

What the conversion actually changes

A SAS date value counts the days since January 1, 1960. A datetime value counts seconds since the start of that day. You have two ways to change how a date appears, and they differ in what is stored:

  • FORMAT statement: changes presentation only. The variable stays numeric, and procedures, sorting and arithmetic still use the underlying day count.
  • PUT function: creates a new character value from the number, using the format you name. The result is stored text. It no longer carries the date's numeric meaning.

This is the main distinction behind most confusion about converting a SAS date to a string. If PROC PRINT shows a nicely formatted date, the column may still be numeric. Check with PROC CONTENTS, which reports the variable's type and attached format.

Basic syntax with PUT

data want;
  set have;
  length date_txt $10;
  date_txt = put(event_date, yymmdd10.);   /* e.g. 2023-10-15 */
run;

The format decides the text layout, and its width sets the string's length. Declaring the length up front with a LENGTH statement makes the column definition explicit. PUT right-aligns numeric formats within the width. If you choose a width larger than the date needs, the result has leading blanks. In that case use a format width that matches the output, or wrap the call in STRIP.

Building a YYYYMM character value

For a year-and-month string with no separator, the YYMMN6. format produces six characters such as 202310:

data want;
  set have;
  length ym $6;
  ym = put(event_date, yymmn6.);
run;

Related formats cover other layouts. YYMMDDN8. gives eight digits with no separators, such as 20231015. YYMMD7. gives a hyphenated year-month, such as 2023-10. DATE9. gives a value such as 15OCT2023. In macro code, %SYSFUNC can apply a format directly. For example, %let ym = %sysfunc(today(), yymmn6.); stores the current year and month as macro text.

Common input problems

  • The source is a datetime, not a date: date formats will misread the seconds count. Extract the date first with DATEPART, for example put(datepart(event_dt), yymmn6.).
  • The source is already character: PUT with a date format will not work on text. Convert it to a number first with INPUT and a matching informat, then apply PUT.
  • Missing dates: PUT on a missing numeric value returns the missing-value character, typically a period, rather than an empty string. Handle missing values explicitly if the destination expects blanks.

Will the string stay readable and usable after export or sharing?

Once the value is character, SAS sends the exact characters you created. The open question is how the receiving system reads them. Several layout choices affect this:

Layout | Example | Sorts chronologically as text | Main caution
YYMMN6. | 202310 | Yes | Spreadsheets may treat it as a plain number
YYMMDD10. | 2023-10-15 | Yes | Some tools auto-convert it back to a date
MMDDYY10. | 10/15/2023 | No | Ambiguous outside the US day-month convention
DATE9. | 15OCT2023 | No | Month abbreviation is English

For localization work, the main risk is ambiguity. A string such as 03/04/2023 means March 4 to US readers and 3 April to many others. A year-first layout avoids this ambiguity and also sorts correctly as plain text. SAS also offers national-language formats such as NLDATE., whose output depends on the session's LOCALE setting. These suit locale-specific display, but the same code can produce different strings in different sessions. Avoid them for keys or values that other systems must parse.

Spreadsheet and database import behavior varies by product, version and import settings. The material supplied for this article includes no tests of specific destinations. Check a sample export in the actual target before you rely on it, and declare the column as text there if it must not be reinterpreted.

When to keep the date numeric

Keep the numeric date, and if needed add the character version alongside it, whenever later steps filter by date range, compute intervals, or need chronological sorting in a layout that does not sort as text. For display-only needs, such as reports, listings and procedure output, a FORMAT statement gives a readable date without discarding the value SAS calculates with.