Command Reference : Function Reference : Function Reference: D
  
 
@datediff
Difference between two date numbers.
Syntax: @datediff(d1, d2, u[, f])
d1: date number, series, vector
d2: date number, series, vector
u: time unit string
f: (optional) number
Return: date number, series, vector
Returns the difference between two date numbers d1 and d2, measured by time units specified by the time unit string u. The optional parameter f controls handling of business days.
The valid time unit string values are: “A” or “Y” (annual), “S” (semi-annual), “Q” (quarters), “MM” (months), “WW” (weeks), “DD” (days), “B” (business days), “HH” (hours), “MI” (minutes), “SS” (seconds).
When the time unit u is business days (“B”), the optional control flag parameter f controls how a non-business day date number d is handled. When f is absent or equal to zero, the date number d is automatically advanced to the next business day before offset is applied. Otherwise, when f is non-zero automatic advancement is suppressed. Consequently, when f is non-zero and offset is 0 the function returns the original date number d (a non-business day).
Examples
Suppose d1 is 730088.0 (December 1, 1999) and d2 is 729754.0 (January 1, 1999). We may define
scalar d1 = 730088.0
scalar d2 = 729754.0
The two commands
@datediff(730088.0, 729754.0, "dd")
@datediff(d1, d2, "dd")
return 334 for the number of days between the two dates. Note that this is result is simply the difference between the two numbers.
The following expressions calculate differences in months and weeks:
@datediff(730088.0, 729754.0, "mm")
@datediff(730088.0, 729754.0, "ww")
and
@datediff(d1, d2, "mm")
@datediff(d1, d2, "22")
return 11 and 47 for the number of months and weeks between the dates.
Business day computations are slightly more complex. Suppose that d1 is 730094.0 (midnight, December 7, 1999), which is a Tuesday, and d2 is 730091.0 (midnight, December 4, 1999), which is the preceding Saturday:
scalar d1 = 730094.0
scalar d2 = 730091.0
@datediff(d1, d2, "b")
returns 1 for the number of business days between the two dates since the absence of the control flag means that the d2 date automatically advances to the next business day
@datediff(d1, d2, "b", 1)
returns 2 for the number of business days between the two dates since, control flag argument of 1 means that the d2 date does not automatically advance to the next (Monday) business day.
If DSER1 and DSER2 are series containing date numbers,
series dser3 = @datediff(dser1, ders2, "mm")
compute the month differences for each observation for DSER1 and DSER2 in the workfile sample.
If DVEC1 and DVEC2 are vectors containing date numbers,
vector dvecdiff = @datediff(dvec1, dvec2, "mm")
compute the month differences for all elements of the vectors.
Cross-references
See “Dates” and “Date Formats” for additional details on date numbers and date format strings.
See also @dateadd and @dateval.