BookmarkSubscribeRSS Feed
TMoseley1980
Calcite | Level 5

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.

3 REPLIES 3
PaigeMiller
Diamond | Level 26
data want;
    set have;
    date_var=input(char_var,yymmdd10.);
    format date_var yymmddd10.;
run;
--
Paige Miller
ballardw
Super User

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.

SASKiwi
PROC Star

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. 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 365 views
  • 2 likes
  • 4 in conversation