Writing a datetime value as m/d/y h:m:s AM/PM


The MDYAMPMw.d format writes datetime values in the following form:

mm/dd/yy hh:mm AM|PM

Since the MDYAMPMw.d informat reads values including seconds, you might want to output a like value that includes seconds.
PROC FORMAT with the PICTURE statement gives the flexibility to create custom formats. See the code in the Full Code section for creating a format that writes output in the following form:

mm/dd/yy hh:mm:ss AM|PM


Full Code

Since the MDYAMPMw.d format doesn't include seconds, PROC FORMAT creates a custom format with output like MDYAMPMw.d format, including seconds.

proc format;
picture myfmt low-high='%m/%d/%Y %I:%0M:%0S %p' (datatype=datetime);
run;

/* If you want single digit months and days to write leading zeros, change */
/* the format definition to this                                           */
proc format;
picture myfmt low-high='%0m/%0d/%Y %I:%0M:%0S %p' (datatype=datetime);
run;

 

/* data read with MDYAMPM and then output with the custom format */
data mine;
  input dt mdyampm22.;
  format dt myfmt.;
datalines;
12/25/2010 1:30:02am
;
proc print;
run;


Output

Obs             dt

 1     12/25/2010 1:30:02 AM