I am trying to convert a date string written without hyphens into a date value. I can do so using the INPUT function if the date is written as "2001-01-01" by calling INPUT("2001-01-01", YYMMDD10.) However, I cannot do the same using YYMMDDN8. or any other format that does not include hyphens.
INPUT("2001-01-01", YYMMDD10.) = 14976 *Correct!
INPUT("20010101", YYMMDDN8.) = 20010101 *Incorrect
INPUT("2001.01.01", YYMMDDP8.) = . *Incorrect
INPUT("2001 01 01", YYMMDDB8.) = . *Incorrect
What am I doing wrong!?
Writing a date with hyphens is causing issues when I write a macro to convert a date string into a date value, so I cannot write the date string that way.
There is no informat YYMMDDN8. or YYMMDDP8. or YYMMDDB8.
data have;
date_without_hyphens='20010101';
date_with_hyphens='2001-01-01';
date_with_periods='2001.01.01';
date_without_hyphens2=input(date_without_hyphens,yymmdd8.);
date_with_hypens2=input(date_with_hyphens,yymmdd10.);
date_with_periods2=input(date_with_periods,yymmdd10.);
run;
Informat YYMMDD10. works for all your date patterns.
data demo;
infile datalines truncover;
input dt_string $20.;
format dt_sas date9.;
dt_sas=input(dt_string,yymmdd10.);
datalines;
2001-01-01
20010101
2001.01.01
2001 01 01
;
proc print data=demo;
run;
@Patrick wrote:
Informat YYMMDD10. works for all your date patterns.
data demo; infile datalines truncover; input dt_string $20.; format dt_sas date9.; dt_sas=input(dt_string,yymmdd10.); datalines; 2001-01-01 20010101 2001.01.01 2001 01 01 ; proc print data=demo; run;
Exactly. The INPUT() function does not care if the WIDTH you used in the INFORMAT is larger than the LENGTH of the string you are reading.
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.