How to convert the datetime character string to SAS datetime value? (ANYDTDTM and MDYAMPM formats)

This post was kindly contributed by StudySAS Blog - go there to comment and to read the full post.


When we have a string like this “9/01/2010 11:52:54 AM” and would like to translate the string to a numeric SAS date time variable, most of the times we use SCAN function to extract the information to get the DATETIME format. This is definitely a tedious job.

SAS formats (MDYAMPM, ANTDTDTM) comes to rescue us. Here is how it works.

data test;

length date $25;

date=”9/01/2010 11:52:54 AM”;

*Convert the character string to SAS datetime value;

datetimevar =input(date,mdyampm25.2);

datetimevar1 =input(date,anydtdtm20.);

*Apply format to the SAS date time value;

format datetimevar datetimevar1 datetime19.;

run;

Result: 01SEP2010:11:52:54

*ANYDTDTM and MDYAMPM informats work together when the datetime value has AM PM specified or day, month, and year components are not ambiguous.

The MDYAMPMw. format writes datetime values with separators in the form mm/dd/yy hh:mm AM PM, and requires a space between the date and the time.

The ANYDTDTM w. format writes…

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]

This post was kindly contributed by StudySAS Blog - go there to comment and to read the full post.