Write Custom Date and Datetime Formats in SAS

As you probably know, SAS dates are merely formated integers. SAS provides tons of out-of-the-box formats for you to use. You can browse them directly in the Format Documentation. However, once in a while, you need a date or datetime format, but you can not find an existing format that fits your needs. Luckily, you can write your own date format with PROC FORMAT. This post will demonstrate a few examples.

The Basics

SAS PROC FORMAT Custom Date Datetime FormatYou can use the Datatype=Date Option to specify that the format is to be applied to a date value. Next, we use the concept of directives to tell SAS how to present the date. There are quite a lot of directives to choose from. Three of them are listed below

  • %A: The full weekday name (Monday, Tuesday, …)

  • %a: The abbreviated weekday name (Mon, Tue, …)

  • %C: Full month name (January, February, …)

You can see the full list of directives in the PROC FORMAT Picture Statement Documentation.

An Example

Let us look at a small example. In the code below, I create the dtfmt picture format. I use the Low-High specification to tell SAS, that I want this format applied to the entire range of numbers. Lets say that I want a format that shows: The entire weekday, a 2 digit number of the day of the month, the entire month name and finally a 4 digit year. I can easily achieve this with the directives %A %D %B %Y. Obviously, I have to use the Datatype=Date option to make the directives be interpreted correctly. Finally, I create a simple list of dates and apply the dtfmt value of them. You can see the result to the right.

proc format; 
  picture dtfmt (default=30)
    low - high = '%A %D %B %Y' (datatype=date)
  ;
run;
 
data test;
    do date='01jan2019'd to '07jan2019'd;
       output;
    end;
run;
 
proc report data=test;
   define date / display left format=dtfmt.;
run;

Be aware that it is good programming practice to control the default length of your formats. I write about this in the post Five Picture Format Options You Should Know.

Language Specific Date Format

I’m from Denmark. Once in a while, I am asked to format dates with Danish weekday names and Danish month names. This is also quite easy to do with PROC FORMAT. You can use the Locale System Option to specify your choice of language. Now, the PROC FORMAT directives will take on the appropriate values of the language. Be aware though, that this works only for English and European languages.

If I want to have Danish week names, month names and so on, I simply run the following code. If I rerun the code above, I will get correct Danish names instead.

options locale=Danish_Denmark;

Summary

I have previously written about user defined formats in the posts Write User Defined Formats in SAS with PROC FORMAT, 5 SAS Picture Format Options You Should Know, Creating Multilabel Formats in SAS with PROC FORMAT and Pass Function Logic to PROC FORMAT in SAS.

You can download the entire code from this post here.