1
votes

I am trying to create a SAS informat for the following date format:

"yyyy-mm-dd hh:ii:ss.SSS UTC", example: "2016-01-14 10:31:01.456 UTC"

I've gotten close using the following format code:

PROC FORMAT;
    PICTURE MyDate other='%0Y-%0m-%0d %0H:%0M:%0s UTC' (datatype=datetime);
RUN;

Unfortunately when I try and use this as an INFORMAT I get an error "can't find format MyDate", as it hasn't been defined as an informat, just an output format.

I can try and create an informat from a dataset created from this format, but due to the milliseconds constraint it will only create values that map to times with .000 in the milliseconds section. For example:

DATA MyInDate ;
    RETAIN FMTNAME "MyInputDate" type "I" ;
    do label = "1jan2016:00:00:00"dt to
               "2feb2016:00:00:00"dt by 1;
        start = trim(left(put(label,MyDate.)));
        output ;
    end ;
RUN;
PROC FORMAT CNTLIN=MyInDate;
RUN;

Even if I were able to enumerate a dataset with milliseconds it would be prohibitively large. Since my dates can span years.

Is it possible to truncate my input data BEFORE passing it to the informat? I don't care about the milliseconds or the UTC qualifier. I can't change the input data.

EDIT: Using anydtdtm. as an informat results in empty values without error messages. Here is the data step making use of this informat:

DATA WORK.ImportDatTest;
LENGTH
    'Event Time'n    8
;
FORMAT
    'Event Time'n    DATETIME25.
;
INFORMAT
    'Event Time'n       anydtdtm.
;
INFILE DATALINES DLM=','
    ;
INPUT
    'Event Time'n    : ANYDTDTM.
;
DATALINES;
2016-01-11 17:23:34.834 UTC
2016-01-11 17:23:34.834 UTC
2016-01-11 17:23:34.834 UTC
;

RUN;
1
What are you trying to do? Informats are used to read dates, not display them but your using it in a PUT statement? Or is that just a demo of your format?Reeza

1 Answers

1
votes

Unfortunately, there is no way to create a picture informat in SAS currently. You would need to convert your data to a format SAS has a built-in informat for, or use a function or similar to format the data.

However, yours already is in such a format, so you shouldn't need to create an informat.

data test;
  x="2015-10-05 10:12:24.333 UTC";
  y=input(x,anydtdtm.);
  put y= datetime17.;
run;

You can certainly truncate data while using an informat; by specifying a length in the informat, it will truncate to that length.

Here's an example using input from datalines:

data test;
  infile datalines dlm=',' dsd;
  input y :anydtdtm32.;
  put y= datetime22.3;
  datalines;
2015-10-05 10:12:24.333 UTC
2014-03-01 08:08:05.435 UTC
2013-01-01 23:02:05.445 UTC
;;;
run;