Friday, March 25, 2016

Hack 2.12 Using the ANYDTDTE Informat to Read Any Date Format


SAS Programming Professionals,

Did you know that you can easily input a plethora of very messy date values using the ANYDTDTE informat?  

This very utilitarian SAS informat can read and extract a host of different date values that are found in real-world data.  Here is an example:                                                                       

data datefile;

input oddates ANYDTDTE32.;
format oddates mmddyy10.; 

datalines;
27APR09
27APR2010
27APR95 14:30:08
27APR2009 14:30:08.5
042769
04272001
97004
2003004
042779
04272099
04/69
04-2007
04/27/11 02:30:08 AM
04/27/2012 02:30:08 AM
APR99
APR2000
030427
19870427
s04Q1
1995Q1
;
run;

proc print noobs data=datefile;
run; 

In this contrived example, we are inputting date values that have been stored in 20 different formats.  The ANYDTDTE informat interprets the input text lines as date values and stores them as SAS dates in the ODDATES variable.  Don’t take my word for it; enter this example into a SAS Display Manager session and take it for a test drive.  As you can see, the ANYDTDTE informat is a handy tool to use when you are reading input files with varying or non-traditional date formats.

The ANYDTDTE informat has two sibling informats:  ANYDTTME which reads and extracts time values from various date, time and datetime forms; and ANYDTDTM, which reads and extracts datetime values from various date, time and datetime forms.  They are also worth looking into!

Best of luck in all of your SAS endeavors!

----MMMMIIIIKKKKEEEE

(aka Michael A. Raithel)
Excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers



I plan to post each and every one of the hacks in the book to social media on a weekly basis.  Please pass them along to colleagues who you know would benefit.