Monday, April 09, 2012

Dates in SAS

Suppose you imported data from a spreadsheet with dates that look like 20120409. How can you convert them to the familiar date9 format in SAS?


If they are already in character format, do the following:


datadate2=input(datadate,mmddyy8.);

format datadate2 date9.;

Take care to replace the mmddyy8. depending on how the date is initially written.


If they are in numeric format, then do the following:


date13dchar=put(date13d,8.);

date13d2=input(date13dchar,yymmdd8.);

format date13d2 date9.;

That is, you first convert numeric to character and then character to date. Be sure to keep in mind that this process is vastly made easier if you take care to input the dates in a "nice" format to begin with in the spreadsheet.