AFHood Group Blog The thoughtless yammerings of marketing junkies..

4Feb/097

SAS date format to YYYYMMDD

One of our many frustrations in life are date values in and out of SAS. Unless you are fortunate enough to be working on projects with standardized dates across the enterprise, you too will run into problems with dates in SAS.

In a large portion of industries, the YYYYMMDD format has become quite popular. It is wonderful in the IT and programming world. We all know you can sort (as in character sort) by this format and get a logical order. So here is a macro to help.

%macro yyyymmdd(DATEVAR) ;
/*--------------------------------------------------------------------
Program Name : yyyymmdd_ah.sas
Purpose      : formats dates into yyyymmdd format
Author       : hooda (www.AFHood.com)
Arguments :

DateVar - SAS date variable

===================================================================
Change History

2009.02.02 - hooda - Macro created

---------------------------------------------------------------------*/

COMPRESS(PUT(&DATEVAR,YYMMDD10.),'-');

/*------ END: AUTOLIB(YYYYMMDD) ----------------*/
%mend ;

Here is an example of how to use this macro.

data test;
date=date();
date1=%yyyymmdd(date);
run;

Now I know what you are thinking.. "You wrote a macro for that?" And the answer is YES. As a group, I find that we use this function excessively. It's just easier for adhoc coding to have it available as a macro. It is effortless to remember "%yyyymmdd()" versus "COMPRESS(PUT(variable,YYMMDD10.),'-');".

I apologize if you think that one is silly, but it has been a favorite among our adhoc coders for years.

Comments (7) Trackbacks (0)
  1. This macro save my day!
    Thanks for sharing :)

  2. Thanks a lot!! This is AWESOME!! :)

  3. Thanks for sharing this. It solves frequent headaches.

  4. this is exactly what i want, really helpful!
    thanks a bunch!

  5. Thanks. This is very helpful.

  6. This is very helpful code…thankx :)

  7. Thx it helps


Leave a comment

No trackbacks yet.