I receive a text file that has timestamps formatted as yyyymmdd:hh:mm:ss. Initially, I thought that the ANYDTDTM. informat would be a perfect choice for reading this data into a SAS datetime value. However, I see some inconsistency with how SAS converts the text into a datetime. Specifically, the inconsistency is with how the date part of the text is interpreted. Consider the following data step. The expected dates are 05Nov2011 and 05Jan2011. The first date is correct, but the 2nd date of 01Nov1920 is certainly wrong. It's as if the first 6 characters (201101) were used to determine the date. But if that were the case, then the first date could be interpreted as 11Nov2011. Can anyone explain this inconsistency?
To add to the confusion, I asked colleagues on my team to run the same datastep and share their results. One of my teammates got the exact opposite outcome. The date of the first obs was 11Nov2011 and the date of the 2nd obs was 05Jan2011.
data test;
infile cards;
input dtm anydtdtm19.;
format dtm datetime19.;
cards;
20111105:20:31:55
20110105:20:31:55
;
run;
proc print;
run;
Results:
Obs dtm
1 05NOV2011:20:31:55
2 01NOV1920:20:31:55
I would raise this one with tech support. In the meantime, doing a bit or pre-processing at least will get you the correct dates. E.g.:
options datestyle=ymd;
data test (drop=_:);
infile cards;
input _dtm $17.;
format dtm datetime19.;
dtm=input(catt(substr(_dtm,1,4),
'-',substr(_dtm,5,2),
'-',substr(_dtm,7,2),
substr(_dtm,9)),YMDDTTM24.);
cards;
20111105:20:31:55
20110105:20:31:55
;
run;
or, a slightly cleaner possible way of accomplishing the desired result:
data test;
infile cards;
informat dtm B8601DT17.;
input @;
substr(_infile_,find(_infile_,':'),1)='T';
input dtm;
format dtm datetime19.;
cards;
20111105:20:31:55
20110105:20:31:55
;
run;
But I tested it. No problem.
Maybe you need to talk to Technical Support.
Ksharp
I tried and got the same error with 9.2.3 on windows with yearcutoff = 1920. When I changed yearcutoff to 1950, I get different error but from the same input value. Get 01NOV2020:20:31:55.
Please talk to tech support.
I believe this issue is related to the fact that ynddttm informat REQUIRES separators between the year, month, and date fields as well at the time fields. Unlike the similar formats mdyampm and datetime where the separators are optional.
data _null_;
input @;
_infile_=prxchange('s/(\d{4})(\d{2})(\d{2})(.*)/\1-\2-\3\4/o',1,_infile_);
put _infile_;
input dtm ymddttm24.;
put dtm datetime19.;
cards;
20111105:20:31:55
20110105:20:41:55
;
run;
2011-11-05:20:31:55
05NOV2011:20:31:55
2011-01-05:20:41:55
05JAN2011:20:41:55
options datestyle=ymd;
data _null_;
input @;
_infile_=prxchange('s/(\d{4})(\d{2})(\d{2})(.*)/\1-\2-\3\4/o',1,_infile_);
put _infile_;
input dtm anydtdtm24.;
put dtm datetime19.;
cards;
20111105:20:31:55
20110105:20:41:55
;
run;
2011-11-05:20:31:55
05NOV2011:20:31:55
2011-01-05:20:41:55
05JAN2011:20:41:55
I eventually reported this issue to tech support. They acknowledged the defect and created the following SAS Note.
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 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: