SAS Dates – Converting to a SAS Date
I find it very frustrating when I forget how to do something in SAS that I know I have done before.
In these times of frustration I type into a Google search bar “SAS how do you do this” and am surprised by how complex all the results you get are. While a lot of the support documents and help guides are very useful and thorough, sometimes I just want to see how you use the function or Proc in simple code and don’t have time to read some of the larger documents.That is why I shall be blogging simple SAS guides for often used and sometimes frustrating code problems.
SAS Dates seems like a good place to start.
SAS Date format conversion basics:
- Each unformatted field in a SAS table can be either a number (Blue circle) or Text (red Triangle)
- When working with Dates in SAS you first want to make sure SAS is reading your date as a SAS date.
- SAS Dates are a number representing the number of days since 01Jan1960. So 1 is 02Jan1960 and 365 is 31st Dec1960.
- To convert a numeric field into a sas date use a combination of input and Put, to convert a text field into a SAS date use input.
- A SAS date in raw form will be a 5 digit number (between 1987 to 2233) but when formatted as a date will show a calendar icon
How to use in code:
DatetoText = put(mydate,format)
Numbertodate = input(put(mydate,format),informat);
- The informat is the format of the field that you are reading in.
- This code will work in both code nodes and wizard nodes (such as the query node)
For a list of SAS Informats go here
For a list of SAS Date Formats go here
P.s While there are many ways of doing anything in SAS the above ways are my preferred way