As you probably know, SAS dates are 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.
You can use the Datatype=Date Option in the Picture Statement 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.
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. 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;
You can edit the code above to create many different custom date and datetime formats. If you want to apply the format to a datetime value, specify Datatype=Datetime Option in the Picture Statement. I encourage you to play around with the code and insert different directives from the documentation.
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 and month names, I simply run the following code. If I rerun the code above, I will get the correct Danish names instead.
In this post, I demonstrate how to write custom date – and datetime formats in SAS. This technique is very flexible and lets the programmer display date/datetime values exactly the way they want. The key is to browse the Picture Statement Documentation and apply the appropriate directives.
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.
Finally, you can download the entire code from this post here.