SAS (Base SAS)+
*=============================================================================;
* Convert character strings to SAS date values
*=============================================================================;
*-----------------------------------------------------------------------------;
* Create sample data with character date strings
*-----------------------------------------------------------------------------;
data raw_dates;
length iso_dt $10 sas_dt $9 us_dt $10;
input iso_dt $ sas_dt $ us_dt $;
datalines;
2025-01-15 15JAN2025 01/15/2025
2025-06-30 30JUN2025 06/30/2025
2025-12-01 01DEC2025 12/01/2025
;
run;
*-----------------------------------------------------------------------------;
* Parse each format using INPUT with the matching informat
*-----------------------------------------------------------------------------;
data parsed_dates;
set raw_dates;
* ISO format YYYY-MM-DD: use yymmdd10.;
dt_from_iso = input(iso_dt, yymmdd10.);
* SAS date9 format ddMONyyyy: use date9.;
dt_from_sas = input(sas_dt, date9.);
* US format MM/DD/YYYY: use mmddyy10.;
dt_from_us = input(us_dt, mmddyy10.);
format dt_from_iso dt_from_sas dt_from_us date9.;
run;
*-----------------------------------------------------------------------------;
* Handle bad data: INPUT with ?? suppresses error notes
*-----------------------------------------------------------------------------;
data bad_dates;
length raw $10;
input raw $;
datalines;
2025-01-15
NOT_A_DATE
2025-13-01
;
run;
data parsed_bad;
set bad_dates;
dt = input(raw, ?? yymmdd10.);
format dt date9.;
run;
proc print data=parsed_dates noobs;
run;
proc print data=parsed_bad noobs;
run;- We create three character date columns in different formats: ISO ("2025-01-15"), SAS date9 ("15JAN2025"), and US slash format ("01/15/2025").
input(iso_dt, yymmdd10.)reads the ISO format; the informat name describes the order: year-month-day, 10 characters wide.input(sas_dt, date9.)reads the SAS-native ddMONyyyy format, which is common in CDISC-related displays.input(us_dt, mmddyy10.)reads the US slash format; this informat handles both slash and dash separators.- Each INPUT call returns a SAS date value (days since 01JAN1960); we apply
format date9.so it displays in human-readable form. - When a character string cannot be parsed, INPUT returns missing and writes a note to the log; adding
??before the informat suppresses those notes. - We demonstrate bad data ("NOT_A_DATE" and an invalid month "2025-13-01") to show that both produce missing with
??in place. - We run PROC PRINT on both datasets and confirm that the three valid dates all resolve to the same underlying value regardless of input format.