DATA Step, Macro, Functions and more

ANYDTDTM Problem?

Reply
Frequent Contributor
Posts: 101

ANYDTDTM Problem?

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

PROC Star
Posts: 7,364

ANYDTDTM Problem?

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=_Smiley Happy;

  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;

PROC Star
Posts: 7,364

ANYDTDTM Problem?

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;

Super User
Posts: 9,687

ANYDTDTM Problem?

But I tested it. No problem.

Maybe you need to talk to Technical Support.

Ksharp

Frequent Contributor
Posts: 129

ANYDTDTM Problem?

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.

Trusted Advisor
Posts: 1,300

ANYDTDTM Problem?

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

Frequent Contributor
Posts: 101

Re: ANYDTDTM Problem?

I eventually reported this issue to tech support. They acknowledged the defect and created the following SAS Note.

http://support.sas.com/kb/46/142.html

Ask a Question
Discussion stats
  • 6 replies
  • 1408 views
  • 1 like
  • 5 in conversation