As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


Dates, times, and datetimes in SAS

From sasCommunity
Revision as of 10:38, 10 January 2018 by Paulkaefer (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

SAS represents Dates, Times and Date-Time combinations as numbers by using special Formats and Informats.

Dates

SAS represents dates as integer numbers. The date 1 January 1960 is the reference date and is represented by the integer value of zero (0). Dates earlier than 1960 are represented by negative integer numbers. The earliest date that SAS can represent is 1 January 1582 and the latest date that SAS can represent is 31 December 19999.

Because SAS uses the Gregorian calendar to calculate dates, it does not handle dates earlier than 1582, the year the Gregorian calendar was proclaimed and implemented by the Catholic church. However, the Gregorian calendar was not adopted immediately in every country. Between 1582 and 1929, most of the countries of the world adopted, or at least recognised this calendar as an official calendar for civil use.

Some care is needed when working with historic dates as the difference between the Gregorian calendar and the previously used Julian calendar can vary by between 10 and 13 days depending on when a country changed from the old style Julian calendar to the new style Gregorian calendar. While the dates in either calendar can be represented by SAS, the calculation of weekdays will be incorrect if a Julian calendar date is used. Also, days between a Julian date and a Gregorian date will be incorrect and may be incorrect even between two Julian dates.

Some significant dates are :

  • The first day of the Gregorian calendar: Friday, 15 October 1582. (The previous day was Thursday, 4 October 1582 in the Julian calendar - a difference of 10 days.) The catholic countries of Spain, Portugal, Poland, and most of Italy adopted the new calendar that day, with France, and the predecessors of modern Belgium and some parts of the Netherlands followed within a year. The colonies of these countries changed their calendars slightly later, as the word of the adoption of the new calendar was spread by the shipping of the day. Other European countries changed to the Gregorian calendar at various dates, and in various ways, over the next 340 years.
  • In countries and colonies under British rule at the time, including the USA, the first Gregorian calendar day is Thursday, 14 September 1752. (The previous day was Wednesday, 2 September 1752 in the Julian calendar - a difference of 11 days.)
  • Alaska first used the Gregorian calendar on Friday, 18 October 1867. (The previous day as Friday, 6 October 1867 in the Julian calendar that was still in use by Russia at the time. The difference was 11 days as well as a time zone shift due, to a change in the international date line when Alaska changed from Russian to US administration.)

Times

Times are represented as seconds since midnight. Times can be real numbers to represent fractions of a second.

Date and Time

Datetimes are represented as seconds since midnight on 1 January 1960. Datetimes can be real numbers to represent fractions of a second. Also, the earliest datetime is midnight on 1 January 1582 and the latest datetime is after 23:59:59 on 31 December 19999 but before midnight on 1 January 20000. Datetime accuracy depends on how many bytes are used for the variable. A minimum length of 6 bytes is required for datetime values.

SAS datetimes do not recognise time zone changes or daylight savings time shifts, although it is relatively sraight forward to calculate daylight savings time or time zone changes using various SAS date and time functions. However, SAS datetimes do not account for or represent leap seconds.

Obtaining Dates and Times from other computer systems and applications

Other computer systems and applications store dates and times in various different ways. When you extract or obtain data from other computer systems you may encounter issues when converting data to a SAS date. Be aware of the following known issues:

Microsoft Excel

Microsoft Excel stores dates as numbers and counts the number of days from 1 January 1900, but it also assumes 1 January 1900 was a leap year, so inserts an additional day for the nonexistent "29 February 1900". Times in Excel are represented as fractions of a day. Excel is not able to represent dates prior to 1 January 1900.

DB2

DB2 has a special Timestamp variable type for storing dates and times, as well as a Date variable type. DB2 uses a proleptic Gregorian calendar that represents dates and times based on ISO 8601. (In ISO 8601 the year can range from 0000 to 9999, so DB2 can represent dates that SAS cannot, while SAS can represent dates that cannot be represented in DB2.

SAS has options that allow dates to be read as text rather than numbers to overcome this problem.

Working with Dates, Times and Datetimes

Because Dates, Times and Datetimes are stored internally as integer numbers you can use Dates, Times and Datetimes variable in calculations to add and subtract periods of time.

Date and Time functions

SAS has a range of functions that manipulate dates, times and datetimes.

See also

Further Reading

The Essential Guide to SAS Dates and Times