SAS Dates, Times, and Datetimes:

by Irina 4. May 2007 09:13

SAS Dates, Times, and Datetimes:

SAS date variables are numeric variables containing integer values. The range of usable values goes from -138,061 (January 01, 1582) to 6,589,335 (December 31, 20000). A value of 0 corresponds to January 01, 1960. The lower limit is due to a change made to the Gregorian calendar in 1582. Most of the SAS date formats are limited to displaying 4 digit years making their limit December 31, 9999, however, the format WORDDATE20. will display 5 digit years. I'm sure that this will be addressed before the year 10,000.

SAS time variables are also numeric variables. SAS times are a count of the number of seconds since midnight. They can and often do contain decimal values representing fractions of a second. The range of values is 0 <= sas time < 86400. A SAS time variable having a value of 60 is 1 minute after midnight. 12:00 noon would be stored internally as 43200. midnight is stored as 0. If a SAS time variable has a value larger than 86400, different formats treat the value differently. The TIMEAMPM. format uses a 12 hour clock so all values of the hour will be between 1 and 12 inclusive. All others, for example, HHMMw. TIMEw. will display the number of hours in the time variable. Below is an example of the differences:

0001
0002  data _null_;
0003    x=100000;
0004    put x timeampm8.;
0005    put x time8.;
0006    put x hhmm8.;
0007  run;

 3:46 AM
27:46:40
   27:47

SAS datetime variables, like the others, are numeric variables. They can have decimal values, like time values, because they have a time component. A SAS datetime value is the number of seconds before or after midnight January 01, 1960. Positive numbers are after that, and negative numbers are before. A datetime value of 253,717,747,199.00 corresponds to 11:59:59 PM on December 31, 9999. This is the upper limit of a usable datetime. Again, greater numbers can be used in calculations, however, they cannot be displayed with currently available formats. SAS can extract the date or time portion of the value through the DATEPART and TIMEPART functions. Date variables can be combined with time variables to create datetime variables with the DHMS function.

The following program uses these three functions to separate the date and time from a datetime value, then recombines them with the DHMS function. Syntax for the DHMS function is :

RESULT=DHMS(date,hour,minute,second);

If you have a time value you do not have to separate out the hours, minutes, and seconds. You can use 0 for the hours and minutes, and use the time variable for the seconds value. This method is used to create datetime2 below.
0001  data _null_;
0002    dt='14jun2004:06:32:30'dt;
0003    date=datepart(dt);
0004    time=timepart(dt);
0005    hh=hour(time);
0006    mm=minute(time);
0007    ss=second(time);
0008    put date= mmddyy10. /
0009        time= timeampm10.
0010        ;
0011    datetime1=dhms(date,hh,mm,ss);
0012    datetime2=dhms(date,0,0,time);
0013    put datetime1= datetime22. /
0014        datetime2= datetime22.
0015        ;
0016  run;

date=06/14/2004
time=6:32:30 AM
datetime1=14JUN2004:06:32:30
datetime2=14JUN2004:06:32:30

Durations:

Durations fall into one of two catagories. Date durations are integer values representing the difference, in number of days between two SAS dates. h3>Interval Functions

SAS provides two interval functions in the DATA step that count and manipulate calendar and clock intervals.

  • INTCK
  • INTNX

Simply stated, INTCK counts intervals and INTNX adjusts dates, times, or datetimes to interval boundaries. There are three types of intervals: single-unit, multiunit, and shifted. The usage and results of the INTCK and INTNX functions will vary somewhat with the different types. All three are covered below.

INTCK

Using the INTCK function, an interval is counted when the boundary is crossed. In the following example the INTCK function is used to count each of the calendar intervals. The starting date is November 30, 2000 and the ending date is January 02, 2001. The result and explanation for each are shown below. The syntax for the INTCK function is:

RESULT=INTCK('INTERVAL',START,END);

0001  data _null_;
0002    start='30nov2000'd;
0003    end = '02jan2001'd;
0004    DAY      = INTCK('DAY      ',start,end);
0005    WEEK     = INTCK('WEEK     ',start,end);
0006    TENDAY   = INTCK('TENDAY   ',start,end);
0007    SEMIMONTH= INTCK('SEMIMONTH',start,end);
0008    MONTH    = INTCK('MONTH    ',start,end);
0009    QTR      = INTCK('QTR      ',start,end);
0010    SEMIYEAR = INTCK('SEMIYEAR ',start,end);
0011    YEAR     = INTCK('YEAR     ',start,end);
0012    put
0013      start    = mmddyy10. /
0014      end      = mmddyy10. /
0015      DAY      = /
0016      WEEK     = /
0017      TENDAY   = /
0018      SEMIMONTH= /
0019      MONTH    = /
0020      QTR      = /
0021      SEMIYEAR = /
0022      YEAR     = /
0023    ;
0024  run;

start=11/30/2000
end=01/02/2001
DAY=33
WEEK=5
TENDAY=4
SEMIMONTH=3
MONTH=2
QTR=1
SEMIYEAR=1
YEAR=1

INTNX

The INTNX function adjusts a SAS date, time, or datetime value forward or backward, and returns the adjusted date value. You also have the ability to specify the alignment. Alignment is where you want the date, time, or datetime adjusted, within the interval. The options are "Beginning", "Middle", and "End". "Beginning" is the default if not specified. The syntax of the INTNX function is:

RESULT=INTNX('INTERVAL',START,INCREMENT<,'ALIGNMENT'>);

Start=04/15/2000
Interval=Year  Increment=-1 Alignment=B Result=01/01/1999
Interval=Year  Increment=-1 Alignment=M Result=07/02/1999
Interval=Year  Increment=-1 Alignment=E Result=12/31/1999

Interval=Qtr   Increment=0  Alignment=B Result=04/01/2000
Interval=Qtr   Increment=0  Alignment=M Result=05/16/2000
Interval=Qtr   Increment=0  Alignment=E Result=06/30/2000

Interval=Month Increment=12 Alignment=B Result=04/01/2001
Interval=Month Increment=12 Alignment=M Result=04/15/2001
Interval=Month Increment=12 Alignment=E Result=04/30/2001

Counting weekdays

0001  data _null_;
0002    do i = 0 to 11;
0003      start=intnx('month','01jan2001'd,i);
0004      end=(intnx('month','01feb2001'd,i))-1;
0005      if 1 < weekday(start) < 7 then startx=start-1;
0006        else startx=start;
0007      count=intck('weekday',startx,end);
0008      put 'In the month of ' start monyy7. ', there are ' count 'weekdays.';
0009    end;
0010  run;

In the month of JAN2001, there are 23 weekdays.
In the month of FEB2001, there are 20 weekdays.
In the month of MAR2001, there are 22 weekdays.
In the month of APR2001, there are 21 weekdays.
In the month of MAY2001, there are 23 weekdays.
In the month of JUN2001, there are 21 weekdays.
In the month of JUL2001, there are 22 weekdays.
In the month of AUG2001, there are 23 weekdays.
In the month of SEP2001, there are 20 weekdays.
In the month of OCT2001, there are 23 weekdays.
In the month of NOV2001, there are 22 weekdays.
In the month of DEC2001, there are 21 weekdays.

About the author

Irina Spivak Irina Spivak
Team Leader at G-Stat. More...


Send mail Email

Blogroll

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2012

    Sign in

    eXTReMe Tracker