# About Date and Time Intervals

### Definitions

duration

• is an integer representing the difference between any two dates or times or datetimes. Date durations are integer values representing the difference, in the number of days, between two SAS dates. Time durations are decimal values representing the number of seconds between two times or datetimes.

Tip: Date and datetimes durations can be easily calculated by subtracting the smaller date or datetime from the larger. When dealing with SAS times, special care must be taken if the beginning and the end of a duration are on different calendar days. Whenever possible, the simplest solution is to use datetimes rather than times.

interval

• is a unit of measurement that SAS can count within an elapsed period of time, such as DAYS, MONTHS, or HOURS. The SAS System determines date and time intervals based on fixed points on the calendar and/or the clock. The starting point of an interval calculation defaults to the beginning of the period in which the beginning value falls , which may not be the actual beginning value specified. For instance, if you are using the INTCK function to count the months between two dates, regardless of the actual day of the month specified by the date in the beginning value, SAS treats it as the first of that month.

### Syntax

SAS provides date, time, and datetime intervals for counting different periods of elapsed time. You can create multiples of the intervals and shift their starting point. Use them with the INTCK and INTNX functions and with procedures that support numbered lists (such as the PLOT procedure). The form of an interval is

name < multiple >< .starting-point >

• The terms in an interval have the following definitions:

name

• is the name of the interval. See the following table for a list of intervals and their definitions.

multiple

• creates a multiple of the interval. Multiple can be any positive number. The default is 1. For example, YEAR2 indicates a two-year interval.

.starting-point

• is the starting point of the interval. By default, the starting point is 1. A value greater than 1 shifts the start to a later point within the interval. The unit for shifting depends on the interval, as shown in the following table. For example, YEAR.3 specifies a yearly period from the first of March through the end of February of the following year.

### Intervals By Category

Table 8.3: Intervals Used with Date and Time Functions

Category

Interval

Definition

Default Starting Point

Shift Period

Example

Description

Date

DAY

Daily intervals

Each day

Days

DAY3

Three-day intervals starting on Sunday

WEEK

Weekly intervals of seven days

Each Sunday

Days (1=Sunday 7=Saturday)

WEEK.7

Weekly with Saturday as the first day of the week

WEEKDAY

< days W>

Daily intervals with Friday-Saturday-Sunday

Each day

Days

WEEKDAY1W

Six-day week with Sunday as a weekend day

counted as the same day (five-daywork week with a Saturday-Sunday weekend). Days identifies the weekend days by number (1=Sunday ... 7=Saturday). By default, days =17.

WEEKDAY35W

Five-day week with Tuesday and Thursday as weekend days (W indicates that day 3 and day 5 are weekend days)

TENDAY

Ten-day intervals (a U.S. automobile industry convention)

First, eleventh, and twenty-first of each month

Ten-day periods

TENDAY4.2

Four ten-day periods starting at the second TENDAY period

SEMIMONTH

Half-month intervals

First and sixteenth of each month

Semi-monthly periods

SEMIMONTH2.2

Intervals from the sixteenth of one month through the fifteenth of the next month

MONTH

Monthly intervals

First of each month

Months

MONTH2.2

February-March, April-May, June-July, August-September, October-November, and December-January of the following year

QTR

Quarterly (three-month) intervals

January 1

April 1

July 1

October 1

Months

QTR3.2

Three-month intervals starting on April 1, July 1, October 1, and January 1

SEMIYEAR

Semiannual (six-month) intervals

January 1

July 1

Months

SEMIYEAR.3

Six-month intervals, March-August and September-February

YEAR

Yearly intervals

January 1

Months

Datetime

To any date interval

Midnight of January 1, 1960

DTMONTH

DTWEEKDAY

Time

SECOND

Second

intervals

Start of the

day (midnight)

Seconds

MINUTE

Minute

intervals

Start of the day (midnight)

Minutes

HOUR

Hourly intervals

Start of the day (midnight)

Hours

#### Example: Calculating a Duration

This program reads the project start and end dates and calculates the duration between them.

` data projects;  options nodate pageno=1 linesize=80 pagesize=60;     input Projid startdate date9. enddate date9.;     Duration=enddate-startdate;     datalines;  398 17oct1997 02nov1997  942 22jan1998 10mar1998  167 15dec1999 15feb2000  250 04jan2001 11jan2001  ;  proc print data=projects;     format startdate enddate date9.;        title 'Days Between Project Start and Project End';  run; `
Output 8.4: Output from the PRINT Procedure ` Days Between Project Start and Project End run                      8  Obs    Projid    Startdate      Enddate    Duration   1       398       17OCT1997  02NOV1997       16   2       942       22JAN1998  10MAR1998       47   3       167       15DEC1999  15FEB2000       62   4       250       04JAN2001  11JAN2001        7 ` ### Boundaries of Intervals

The SAS System associates date and time intervals with fixed points on the calendar. For example, the MONTH interval represents the time from the beginning of one calendar month to the next, not a period of 30 or 31 days. When you use date and time intervals (for example, with the INTCK or INTNX functions), the SAS System bases its calculations on the calendar divisions that are present. Consider the following examples:

Table 8.4: Using INTCK And INTNX

Example

Results

Explanation

mnthnum1=

intck(

'month',

'25aug2000'd,

'05sep2000'd);

mnthnum1=1

The number of MONTH intervals the INTCK function counts depends on whether the first day of a month falls within the period.

mnthnum2=

intck(

'month',

'01aug2000'd,

'31aug2000'd);

mnthnum2=0

next=intnx(

'month',

'25aug2000'd,1);

next represents 01sep2000

The INTNX function produces the SAS date value that corresponds to the beginning of the next interval.

Note: The only intervals that do not begin on the same date in each year are WEEK and WEEKDAY. A Sunday can occur on any date because the year is not divided evenly into weeks.

### Single-Unit Intervals

Single-unit intervals begin at the following points on the calendar:

Table 8.5: Single-Unit Intervals

These single-unit intervals

Begin at this point on the calendar

DAY and WEEKDAY

each day

WEEK

each Sunday

TENDAY

the first, eleventh, and twenty-first of each month

SEMIMONTH

the first and sixteenth of each month

MONTH

the first of each month

QTR

the first of January, April, July and October

SEMIYEAR

the first of January and July

YEAR

the first of January

Single-unit time intervals begin as follows :

Table 8.6: Single-Unit Time Intervals

These single-unit time intervals

Begin at this point

SECOND

each second

MINUTE

each minute

HOUR

each hour

### Multiunit Intervals

#### Multiunit Intervals Other Than Multiweek Intervals

Multiunit intervals, such as MONTH2 or DAY50, also depend on calendar measures, but they introduce a new problem: the SAS System can find the beginning of a unit (for example, the first of a month), but where does that unit fall in the interval? For example, does the first of October mark the first or the second month in a two-month interval?

For all multiunit intervals except multiweek intervals, the SAS System creates an interval beginning on January 1, 1960, and counts forward from that date to determine where individual intervals begin on the calendar. As a practical matter, when a year can be divided evenly by an interval, think of the intervals as beginning with the current year. Thus, MONTH2 intervals begin with January, March, May, July, September, and November. Consider this example:

Table 8.7: Month2 Intervals

SAS statements

Results

howmany1=intck

('month2','15feb2000'd,'15mar2000'd);

howmany1=1

count=intck

('day50','01oct2000'd,'01jan2000'd);

count=1

In the above example, the SAS System counts 50 days beginning with January 1, 1960; then another 50 days; and so on. As part of this count, the SAS System counts one DAY50 interval between October 1, 1998 and January 1, 1999. As an example, to determine the date on which the next DAY50 interval begins, use the INTNX function, as follows:

Table 8.8: Using the INTNX Function

SAS statements

Results

start=intnx

('day50','01oct98'd,1);

SAS date value 14200, or Nov 17, 1998

The next interval begins on November 17, 1998.

Time intervals (those that represent divisions of a day) are aligned with the start of the day, that is, midnight. For example, HOUR8 intervals divide the day into the periods 00:00 to 08:00, 8:00 to 16:00, and 16:00 to 24:00 (the next midnight).

#### Multiweek Intervals

Multiweek intervals, such as WEEK2, present a special case. In general, weekly intervals begin on Sunday, and the SAS System counts a week whenever it passes a Sunday. However, the SAS System cannot calculate multiweek intervals based on January 1, 1960, because that date fell on a Friday, as shown: Figure 8.2: Calculating Multi Week Intervals

Therefore, the SAS System begins the first interval on Sunday of the week containing January 1, 1960-that is, on Sunday, December 27, 1959. The SAS System counts multiweek intervals from that point. The following example counts the number of two-week intervals in the month of August, 1998:

Table 8.9: Counting Two-Week Intervals

SAS statements

Results

count=intck

('week2','01aug98'D, '31aug98'D);

count=3

To see the beginning date of the next interval, use the INTNX function, as shown here:

Table 8.10: Using INTNX to See The Beginning Date of an Interval

SAS statements

Results

begin=intnx('week2','01aug1998'd,1);

'Begin' represents SAS date 14093 or August 02, 1998

The next interval begins on August 16.

### Shifted Intervals

Shifting the beginning point of an interval is useful when you want to make the interval represent a period in your data. For example, if your company's fiscal year begins on July 1, you can create a year beginning in July by specifying the YEAR.7 interval. Similarly, you can create a period matching U.S. presidential elections by specifying the YEAR4.11 interval. This section discusses how to use shifted intervals and how the SAS System creates them.

#### How to Use Shifted Intervals

When you shift a time interval by a subperiod, the shift value must be less than or equal to the number of subperiods in the interval. For example, YEAR.12 is valid (yearly periods beginning in December), but YEAR.13 is not. Similarly, YEAR2.25 is not valid because there is no twenty-fifth month in the two-year period.

In addition, you cannot shift an interval by itself. For example, you cannot shift the interval MONTH because the shifting subperiod for MONTH is one month and MONTH contains only one monthly subperiod. However, you can shift multi-unit intervals by the subperiod. For example, MONTH2.2 specifies bimonthly periods starting on the first day of the second month.

#### How the SAS System Creates Shifted Intervals

For all intervals except those based on weeks, the SAS System creates shifted intervals by creating the interval based on January 1, 1960, by moving forward the required number of subperiods, and by counting shifted intervals from that point. For example, suppose you create a shifted interval called DAY50.5. The SAS System creates a 50-day interval in which January 1, 1960 is day 1. The SAS System then moves forward to day 5. (Note that the difference , or amount of movement, is 4 days.) The SAS System begins counting shifted intervals from that point. The INTNX function demonstrates that the next interval begins on January 5, 1960:

Table 8.11: Using INTNX to Determine When an Interval Begins

SAS statements

Results

start=intnx

('day50.5','01jan1960'd,1);

SAS date value 4, or Jan 5, 1960

For shifted intervals based on weeks, the SAS System first creates an interval based on Sunday of the week containing January 1, 1960 (that is, December 27, 1959), then moves forward the required number of days. For example, suppose you want to create the interval WEEK2.8 (biweekly periods beginning on the second Sunday of the period). The SAS System measures a two-week interval based on Sunday of the week containing January 1, 1960, and begins counting shifted intervals on the eighth day of that. The INTNX function shows the beginning of the next interval:

Table 8.12: Using the INTNX Function to Show the Beginning of the Next Interval

SAS statements

Results

start=intnx

('week2.8','01jan1960'd,1);

SAS date value 2, or Jan 3, 1960

You can also shift time intervals. For example, HOUR8.7 intervals divide the day into the periods 06:00 to 14:00, 14:00 to 22:00, and 22:00 to 06:00. SAS 9.1 Language Reference Concepts
ISBN: 1590471989
EAN: 2147483647
Year: 2004
Pages: 255