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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1985 views
  • 0 likes
  • 3 in conversation