W 10-04-12
;
proc print data=meeting;
format mtg sendmail date9.;
title 'When To Send Announcements';
run;
Output 7.3 Calculated Date Values: When to Send Mail
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.
T I P 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. SAS determines date and time intervals based on
fixed points on the calendar, the clock, or both. The starting point of an interval
calculation defaults to the beginning of the period in which the beginning value falls,
which might not be the actual beginning value specified. For example, 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.
About Date and Time Intervals 123
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). This is the form of an interval:
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 7.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
<daysW>
Daily intervals
with Friday-
Saturday-
Sunday
Each day Days WEEKDAY1W Six-day week
with Sunday
as a weekend
day
124 Chapter 7 Dates, Times, and Intervals
Category Interval Definition
Default
Starting
Point Shift Period Example Description
counted as the
same day (five-
day work 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 Months QTR3.2 Three-month
intervals
starting on
April 1, July
1, October 1,
and January 1
April 1
July 1
October 1
About Date and Time Intervals 125
Category Interval Definition
Default
Starting
Point Shift Period Example Description
SEMIYEAR Semiannual
(six-month)
intervals
January 1 Months SEMIYEAR.3 Six-month
intervals,
March-
August, and
September-
February
July 1
YEAR Yearly intervals January 1 Months
Datetime Add DT to any
of the date
intervals
Interval
corresponding
to the
associated 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. Then, the program calculates the
duration between them.
options nodate pageno=1 linesize=80 pagesize=60;
data projects;
input Projid @5 startdate date9. @15 enddate date9.;
Duration=enddate-startdate;
datalines;
398 17oct1997 02nov1997
942 22jan1998 11mar1998
167 15dec1999 15feb2000
250 04jan2001 11jan2001
;
proc print data=projects;
format startdate enddate date9.;
title 'Days Between Project Start and Project End';
run;
126 Chapter 7 Dates, Times, and Intervals

Get SAS 9.4 Language Reference, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.