One of the most common pain points for anyone working with SAS is date formats. Depending on your requirements, it can be tricky either formatting your data properly, or converting / extracting dates into the correct format your project needs. Eventually, I noticed that I was spending way too much time googling unique SAS date formats so eventually I started jotting them down with examples and assembled my own cheat sheet. Having a quick scratch reference like this not only saved a ton of time, it forced me to learn a lot of these date quirks that drove me insane before.
The DATEw. format writes SAS date values in the form ddmmmyy, ddmmmyyyy, or dd-mmm-yyyy, where:
dd – is an integer that represents the day of the month.
mmm – is the first three letters of the month name.
yy or yyyy – is a two-digit or four-digit integer that represents the year.
Some examples:
/*#date.w format w=width# */ put day date5.; /*16MAR*/ put day date7.; /*16MAR08 */ put day date8.; /* 16MAR08 */ put day date9.; /* 16MAR2008 */ put day date11.; /*16-MAR-2008 */
MONYY format writes date values as the month and year.
/*monyyw. format */ format w=width# */ put date monyy5.; /*DEC05*/ put date monyy7.; /*DEC2005*/
The MMDDYY format writes dates with integers for date and month values, along with a seperator (/)
/*MMDDYY. format */ format w=width# */ put date MMDDYY8.; 10/15/15 /* 8 Characters */ put date MMDDYY10; 10/15/2015 put date MMDDYY5; 10/15/15
Finally, we have the The YYMMDDw. format. This one format writes SAS date values in the form yymmdd or yy-mm-dd, where:
yy is a two-digit or four-digit integer that represents the year.
– is the separator.
mm is an integer that represents the month.
dd is an integer that represents the day of the month.
put day yymmdd8.; /*16-11-03*/ put day yymmdd10.; /*2016-11-03*/
If you’ve read any SAS tutorials or taken any training, chances are the first thing you learned is, “Always Read the Log”. The same advice applies when you are debugging date formats. For starters, if you are creating macro variables with any date values, you’ll probably want to be able to see what those macro variables are resolving to in the log. You can turn that on with the symbolgen option:
options symblogen;
Also, it helps to get into the habit of outputting the values for your date macro variables using a data null step at the beginning of your program, just so you have a consolidated view of the date variables available to you, as well as what date(s) they represent and the formats that they are in:
data _null_; put "**********DATE PARAMS**********"; put "yesterday is &yest"; put "one week ago is &one_week"; put "one year ago is &one_year"; put "*******************************"; run;
For most of us, unless you have a rainmain like memory, it will be difficult to remember every specific format and its quirks. That’s why I’d encourage you to use or print some of the examples above for your own cheatsheet, or consult the Documentation for a reference on formats/informats (not just date formats). If these don’t meet your needs, more often than not there are some good SUGI papers out there or a Stackoverflow thread that can help you out with date formats. They key takeaway here is that you aren’t alone, we have all fought with SAS date formats from time to time. Making your own cheatsheet is a great way to commit some of your most frequently used formats to memory, and save you a good bit of time in the future.