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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.