Search notes:

SAS: date

Dates are stored as numbers

SAS dates are stored as numbers of days since January 1, 1960. So, January 1st, 1960 is actually the number 0, January 2nd 1960 the number 1, December 31st 1960 as 365 (leap year!).
data _null_;

     jan_01_1960 = '01jan1960'd;
     jan_02_1960 = '02jan1960'd;
     dec_31_1960 = '31dec1960'd;
     
     put jan_01_1960= 4.; /*   jan_01_1960=0     */
     put jan_02_1960= 4.; /*   jan_02_1960=1     */
     put dec_31_1960= 4.; /*   dec_31_1960=365   */

run;
Github repository about-SAS, path: /programming/date-and-time/days-since-1960.sas

Times are also stored as numbers

A time variable is independant of a date: it stores the number of seconds since midnight. So, 0:01 AM is stored as 60, 1:00 AM as 3600, 10:00 AM as 36000 etc.
data _null_;

     am_00_01 = '00:01't;
     am_01_00 = '01:00't;
     am_10_00 = '10:00't;
     
     put am_00_01=;  /* am_00_01=60     */
     put am_01_00=;  /* am_01_00=3600   */
     put am_10_00=;  /* am_10_00=36000  */

run;
Github repository about-SAS, path: /programming/date-and-time/seconds-since-midnight.sas

Date times

A date time variable stores both: a date and a time.
data dates;

  format d       8.
         d_as_dt date9.
         d_as_ts datetime20.
         d_as_tm time9.;

  do d = 0 to 5;
     d_as_dt = d;  /* d counts Days since January 1st, 1960                               */
     d_as_ts = d;  /* d counts Seconds since January 1s, 1960, Midnight                   */
     d_as_tm = d;  /* d counts Seconds since Midnight, without specifying a specific date */
     output;
  end;

run;


proc sql;
/*   Describing the data set "dates" verifies that
     the dates are internally stored as num's */
  describe table dates;
/*

  create table WORK.DATES( bufsize=65536 )
  (
   d num format=8.,
   d_as_dt num format=DATE9.,
   d_as_ts num format=DATETIME20.,
   d_as_tm num format=TIME9.
  );

*/
quit;
Github repository about-SAS, path: /programming/date-and-time/dates-are-numbers.sas
See also: Formatting a date time with proc format and and the datetime. format.

TODO

Data variables are valid for dates since 1582 (adoption of the Gregorianic Calendar).

Index