IBM Support

Adding days, weeks months or years to a date variable

Troubleshooting


Problem

I have a date variable in SPSS for Windows. I would like to add a number of days, weeks, or months to that variable. How can I do that?

Resolving The Problem

Please use the DATE.DAYS command to add a given number of days to a date variable. For example, if I wanted to add a week to a date variable, I could do the following:

COMPUTE datevar_1=datevar + time.days (7).
EXECUTE.

The above syntax assumes that DATEVAR is an SPSS variable in some sort of date format (such as DATE or ADATE). The number in parentheses indicates the number of days you wish to add to the original date value. To create a variable that is one week later than our DATEVAR date variable, enter 7 in parentheses. Forr two weeks, change the value to in parentheses to 14, and so on.

To add months to a date value, you will wish to use the DATESUM function in SPSS. For example, if I want to add two months to a date variable, I may use the following syntax:

COMPUTE datevar2=datesum(datevar,2,"MONTHS").
EXECUTE.
FORMATS datevar2 (adate8).

The DATESUM function is useful because it takes into account the different lengths of different months. So, for example, with the above syntax a DATEVAR value of 12/31/06 would return a DATEVAR2 value of 2/28/07. Please go to the Help - Topics window in SPSS, click on the Topics tab, and enter "Date increments" in the search field. Look for the "Date Increments" topic and you will find more information regarding the DATESUM function.

You may also use a negative number in the parentheses of the DATESUM function to subtract a period of time. For example,

COMPUTE datevar2=datesum(datevar,-2,"MONTHS").
EXECUTE.

DATEVAR2 will return a value that is two months prior to the DATEVAR variable.

[{"Product":{"code":"SSLVMB","label":"IBM SPSS Statistics"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Component":"Not Applicable","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"Not Applicable","Edition":"","Line of Business":{"code":"LOB10","label":"Data and AI"}}]

Historical Number

71169

Document Information

Modified date:
16 April 2020

UID

swg21478143