Date increments

The DATESUM function calculates a date or time value a specified number of units from a given date or time value. The general form of the function is:

DATESUM(datevar, value, "unit", "method").
  • datevar is a date/time format variable (or a numeric value that represents a valid date/time value).
  • value is a positive or negative number. For variable-length units (years, quarters, months), fractional values are truncated to integers.
  • "unit" is one of the following string literal values enclosed in quotes: years, quarters, months, weeks, days, hours, minutes, seconds.
  • "method" is an optional specification for variable-length units (years, quarters, months) enclosed in quotes. The method can be "rollover" or "closest". The rollover method advances excess days into the next month. The closest method uses the closest legitimate date within the month. This is the default.

Example

DATA LIST FREE /datevar1 (ADATE10).
BEGIN DATA
2/28/2004
2/29/2004
END DATA.
COMPUTE rollover_year=DATESUM(datevar1, 1, "years", "rollover").
COMPUTE closest_year=DATESUM(datevar1, 1, "years", "closest").
COMPUTE fraction_year=DATESUM(datevar1, 1.5, "years").
FORMATS rollover_year closest_year fraction_year (ADATE10).
SUMMARIZE 
 /TABLES=datevar1 rollover_year closest_year fraction_year
 /FORMAT=VALIDLIST NOCASENUM
 /CELLS=NONE.
Figure 1. Results of rollover and closest year calculations
Results of rollover and closest year calculations
  • The rollover and closest methods yield the same result when incrementing February 28, 2004, by one year: February 28, 2005.
  • Using the rollover method, incrementing February 29, 2004, by one year returns a value of March 1, 2005. Since there is no February 29, 2005, the excess day is rolled over to the next month.
  • Using the closest method, incrementing February 29, 2004, by one year returns a value of February 28, 2005, which is the closest day in the same month of the following year.
  • The results for fraction_year are exactly the same as for closest_year because the closest method is used by default, and the value parameter of 1.5 is truncated to 1 for variable-length units.
  • All three COMPUTE commands create new variables that display values in the default F format, which for a date value is a large integer. The FORMATS command specifies the ADATE format for the new variables.

Example

DATA LIST FREE /datevar1 (ADATE10).
BEGIN DATA
01/31/2003
01/31/2004
03/31/2004
05/31/2004
END DATA.
COMPUTE rollover_month=DATESUM(datevar1, 1, "months", "rollover").
COMPUTE closest_month=DATESUM(datevar1, 1, "months", "closest").
COMPUTE previous_month_rollover = 
  DATESUM(datevar1, -1, "months", "rollover").
COMPUTE previous_month_closest = 
  DATESUM(datevar1, -1, "months", "closest").
FORMATS rollover_month closest_month 
  previous_month_rollover previous_month_closest (ADATE10).
SUMMARIZE
  /TABLES=datevar1 rollover_month closest_month
   previous_month_rollover previous_month_closest
  /FORMAT=VALIDLIST NOCASENUM
  /CELLS=NONE.
Figure 2. Results of month calculations
Results of month calculations
  • Using the rollover method, incrementing by one month from January 31 yields a date in March, since February has a maximum of 29 days; and incrementing one month from March 31 and May 31 yields May 1 and July 1, respectively, since April and June each have only 30 days.
  • Using the closest method, incrementing by one month from the last day of any month will always yield the closest valid date within the next month. For example, in a nonleap year, one month after January 31 is February 28, and one month after February 28 is March 28.
  • Using the rollover method, decrementing by one month (by specifying a negative value parameter) from the last day of a month may sometimes yield unexpected results, since the excess days are rolled back to the original month. For example, one month prior to March 31 yields March 3 for nonleap years and March 2 for leap years.
  • Using the closest method, decrementing by one month from the last day of the month will always yield the closest valid date within the previous month. For example, one month prior to April 30, is March 30 (not March 31), and one month prior to March 31 is February 28 in nonleap years and February 29 in leap years.