BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sas_nut
Calcite | Level 5

Hi, I have a text file with a date with multiple date formats.  Kind of what's created below in the sample data.  I also created a picture format for the date format I need (further below).  What I can't figure out is how to conditionally read in the file and maintain the data as a date.  The final output needs to have the new date format (newdate.).  I'm trying to avoid reading it in as a string then converting it later.  Any ideas?

Thanks!

 

*sample data;

data test;
input date $10.;
datalines;
01JUL2020
2020-07-01
;
run;

 

*new data format.

proc format;
picture NewDate other='%Y-%0m-%0d' (datatype=date);
run;
%let now=%sysfunc(today(),newdate.);
%put &now;

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

No need for custom formats:

data test;
input date anydtdte10.;
format date yymmddd10.;
datalines;
01JUL2020
2020-07-01
;

Although I would rather have (preferred in this order):

  • a consistent date format
  • a limited, exactly specified list of acceptable date formats, which can be checked
  • the current garbage that WILL lead to a problem some time in the future

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

No need for custom formats:

data test;
input date anydtdte10.;
format date yymmddd10.;
datalines;
01JUL2020
2020-07-01
;

Although I would rather have (preferred in this order):

  • a consistent date format
  • a limited, exactly specified list of acceptable date formats, which can be checked
  • the current garbage that WILL lead to a problem some time in the future
sas_nut
Calcite | Level 5
Thank you!
ballardw
Super User

If you have two-digit years in the first position they may be treated as dd-mm-yy instead of yy-mm-dd values OR mm-dd-yy, national language rules, and may have missing values using the ANYDTDTE infromat.

So double check any results with 2 digit years, if any, from your source file, and the range of result for sensibility. If you end up asking "how did that date get in there" these are candidates for odd values in your input and the coding rules for ANYDTDTE

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1322 views
  • 0 likes
  • 3 in conversation