I have a lot of date variables that came out of a database stored as text, in the format YYYY-MM-DD real example : 1998-01-15
. Found examples, and I've found a list of date formats, but none of them match this. The data starts in 1997, so all four year digits are important.
Thanks for your help.
data want;
set have;
date_var=input(char_var,yymmdd10.);
format date_var yymmddd10.;
run;
Some of the date informats (you don't want a format to read data) are pretty tolerant of characters as long as they are used consistently, as is same between year and month and day.
data example; input string :$10.; date_var = input(string,yymmdd10.); format date_var yymmddd10.; datalines; 1998-01-15 1998/01/15 1998*01*15 1998!01!15 1998#01#15 1998\01\15 1998|01|15 1998$01$15 1998^01^15 1998(01(15 1998%01%15 1998&01&15 19980115 ;
While I wouldn't expect to see some of those in most files you might be surprised some time.
Actually you concern about 4 digit years is well founded and should be thankful all four are provided. It is the idiots that dump something like 010203 with two digit years and you don't know if that is supposed to be 3 Feb 2001 (or 3 Feb 1901), 2 Mar 2001, 1 Feb 2003 or 2 Jan 2003.
How are these date variables stored in the source database? If their column types are date or datetime at source it could be easier to fix the extract process rather than bandaid the data afterwards.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.