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

September 16, 2010
By

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.

Tags: , , , , , , , , , ,

Welcome!

SAS-X.com offers news and tutorials about the various SAS® software packages, contributed by bloggers. You are welcome to subscribe to e-mail updates, or add your SAS-blog to the site.

Sponsors





Dear readers, proc-x is looking for sponsors who would be willing to support the site in exchange for banner ads in the right sidebar of the site. If you are interested, please e-mail me at: tal.galili@gmail.com
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration.