![]() |
|
Goto the Tip of the Month Archive Other interesting pages ...
LinkedIn Profile |
SAS Tip of the Month Dates and times are something programmers deal with every day. It is easy to see a date and/or time in character form, but how does SAS store that date in a numeric form? To bring dates or times in character format, SAS has many ways that this can be done, but here are some examples: Date/Time Text SAS Code 12MAY2014 var=INPUT(text,DATE9.); 12MAY14 var=INPUT(text,DATE7.); 12/05/2014 var=INPUT(text,DDMMYY10.); 05/12/2014 var=INPUT(text,MMDDYY10.); MAY2014 var=INPUT(text,MONYY7.); 2014-05-12 var=INPUT(text,YYMMDD10.); 15:32 var=INPUT(text,TIME5.); 12MAY2014:15:32. var=INPUT(text,DATETIME15.); The second parameter to the INPUT function is called an INFORMAT which tells SAS how to read the text – SAS has a load of them but the ones above are the most common for bringing in date and/or times from a text string. Now how does SAS store these dates and times numerically? For dates, “day 0” is January 1st, 1960 and has a value of zero. The date range that is possible in SAS is year 1582 to year 19,900. As the date is stored as a number it is easy to calculate durations, i.e. day number of one date minus the day number of another date. For times, this is the number of seconds from mid-night, and has a range of 0 to 86,400 (there are 86,400 seconds in a day). Lastly, a SAS datetime value is a value representing the number of seconds between January 1, 1960 and an hour/minute/second within a specified date. However, knowing these numbers, it is impossible to see what date and/or time it is. Above we used INFORMATS to read the dates in, now we can display them using some formats: FORMAT Name Display DATE9. 12MAY2014 TIME5. 15:32 DATETIME15. 12MAY2014:15:32 Occasionally I will get a datetime variable but I just need the date portion for the program – this can be done using the DATEPART function as the following code illustrates: var = DATEPART(datetime_numeric_variable); I hope this small introduction into the world of dates and times was useful. |
________________________________ Updated January 2, 2015 |