Search code examples
datetimeposixsas

SAS datetime formatting as POSIX


I want SAS to format datetimes in a POSIX format. The following gives "2009-11-25 14:44:56" how can I display the milliseconds ?

proc format;
    picture POSIX other='%0Y-%0m-%0d %0H:%0M:%0S' (datatype=datetime);
run;
data test;
  mydatetime='25nov2009 14:44:56.300'dt;
  format newdt POSIX.;
  newdt=mydatetime;
  put mydatetime= newdt=;
run;

EDIT : I am even taker of ANY way to format datetimes to YYYY-MM-DD HH:M:SS.sss (ISO8601) amazing I can't find this in less than 1 minute


Solution

  • E8601DT23.3 should display your values as you wish, except for the extra "T" separator; SAS seems to require that. If you're putting to a character value you can remove the "T" easily; if you are trying to use a formatted numeric value, I think you would have to live with it.

    data test;
      mydatetime='25nov2009 14:44:56.356'dt;
      format newdt E8601DT23.3;
      newdt=mydatetime;
      put mydatetime= newdt=;
    run;
    

    SAS guide on ISO8601:

    http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a003169814.htm

    Edit: SAS-L came through on this one ( http://listserv.uga.edu/cgi-bin/wa?A1=ind1303c&L=sas-l#8 ). If you have 9.3 this should work (not in 9.2 or earlier).

    proc format;
        picture POSIX other='%0Y-%0m-%0d %0H:%0M:%0s' (datatype=datetime);
    run;
    data test;
      mydatetime='25nov2009 14:44:56.300'dt;
      format newdt POSIX23.3;
      newdt=mydatetime;
      put mydatetime= newdt=;
    run;
    

    Little s, and include the decimal, and it should work as expected. Thanks to data null for the tip.