SPSS tutorials website header logo SPSS TUTORIALS BASICS ANOVA REGRESSION FACTOR CORRELATION

SPSS – Extract ISO Weeks from Date Variable

For 2022, the Spring holidays in the Netherlands fall in week 8. Now, this “week 8” refers to ISO week 8, 2022, which runs from 21 through 28 February 2022.

Now, if we've an SPSS data file containing dates, how can we extract ISO week numbers from those? We'll quickly walk you through.

Creating Test Data

Let's first create some test data: the syntax below creates an SPSS date variable for today and its previous 999 days.

*CREATE DATE VARIABLE FOR TODAY + PREVIOUS 999 DAYS.

input program.
loop mydate = 1 to 1000.
end case.
end loop.
end file.
end input program.
execute.

compute mydate = datesum($time,-mydate + 1,'days').
execute.

formats mydate (date11).

Result

SPSS Extract Isoweek From Date Test Data

SPSS’ Nonsensical Week Numbers

Right. Now, SPSS seems able to extract week numbers, which is done with something like compute week = xdate.week(mydate). Sadly, these week numbers are nonsensical and useless as discussed in SPSS Computes Wrong Week Numbers? For example, some weeks may consist of only a single day as shown below.

SPSS Week Numbers Wrong

In short: SPSS can't extract “normal” week numbers from dates. However, Python presents us with a workaround.

Week Numbers from Python Isocalendar

Python's datetime module contains an ISO calendar. Given a Python datetime object, this returns a tuple containing its ISO year, week and day.

Fortunately, the datetime module is part of the SPSS-Python-essentials so there's no need for installing it. The syntax below presents a very quick example for how to use it.

*CREATE PYTHON DATETIME OBJECT AND EXTRACT ISOCALENDAR FROM IT.

begin program python3.
import datetime
myDate = datetime.datetime(2022,1,1,8,31,0)
print("My date is {}.".format(myDate))
print("But my ISOdate is {}.".format(myDate.isocalendar()))
end program.

Result

Python Datetime Isocalendar Result

Note that 1 January 2022 falls in ISO week 52 for 2021. Our Google calendar confirms that this is correct as shown below.

Isoweeks In Google Calendar

Right. So now we're left with 2 challenges:

The first step is done with the CvtSpssDatetime function from the spssdata module. Next up, we can apply any Python function to one or many SPSS variables with an SPSS-extension: Programmability Transformation as found under Transform SPSS Menu Arrow Programmability Transformation If this is not present in your SPSS version, you may download and intall SPSSINC_TRANS.spe from this Github page. Let's now apply both fixes with our final syntax below.

Final Syntax

*DEFINE FUNCTION FOR ISOWEEK FROM SPSS DATE.

begin program python3.
import datetime
from spssdata import CvtSpssDatetime
def iso(spssdate):
    return CvtSpssDatetime(spssdate).isocalendar()
end program.

*APPLY FUNCTION TO SPSS VARIABLE.

spssinc trans result=isoyear isoweek isoday TYPE = 0
/formula "iso(mydate)".

Result

SPSS Extract Isoweek From Date Result

As shown, we have now correctly added the ISO year, week and day for our SPSS-date-variable.

Final Notes

Before I round off, I'd like to emphasize that SPSSINC TRANS is useful for many more interesting challenges such as

This extension really deserves a separate tutorial and I may write just that if my time will permit it.

Credits

I'd like to thank my dear colleague Jon Peck for creating the SPSSINC TRANS extension as well as the final syntax for this tutorial.

Tell us what you think!

*Required field. Your comment will show up after approval from a moderator.