miércoles, 15 de agosto de 2012

Converting SAS date formats

A typical problem when working with any kind database with temporal records, is transforming dates to a human-readable. Another typical problem arises when we work with different data sources that manage dates in very different formats, for example an Excel datasheet and a Oracle database, in which case we need to normalize the formats before we start putting our information together.

Unfortunately, dates in SAS are a bit hard to understand, and I have seen a lot of people having trouble trying to make sense of them, specially those whitout a strong technical background and who are not so familiar with complex programming structures. However if you are fluent in Perl regular expressions and C pointers, then SAS dates should be piece of cake!

In order to simplify the understanding of SAS formats, I put together a little piece of code with all the usual conversions between SAS, Oracle and Human readable formats (typically YYYYMMDD), which serves as a reference table to know how we convert from one format to another.

/* From human-readable format YYYYMMDD to SAS and Oracle */
data test_dates; 
  mydate        = '20111201'; 
  mydate_sas    =  input(mydate, yymmdd8.); 
  mydate_oracle =  dhms(mydate_sas, 0,0,0); 
run;
/* From SAS to Oracle and YYYYMMDD*/ 
data fechas; 
  mydate_sas    = today(); 
  mydate        = put(mydate_sas, yymmddn8.); 
  mydate_oracle = dhms(mydate_sas, 0,0,0); 
run;
/* From Oracle to SAS and YYYYMMDD */ 
data fechas; 
set oracle_table; 
  mydate_sas    = datepart(mydate_oracle); 
  mydate        = put(mydate_sas, yymmddn8.); 
run;

These little examples will help you identifying the function you need to convert from one format to another. Good luck with your date conversions and enjoy your new Rosetta stone for SAS!

No hay comentarios:

Publicar un comentario