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.
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’ 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.
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.
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
Note that 1 January 2022 falls in ISO week 52 for 2021. Our Google calendar confirms that this is correct as shown below.
Right. So now we're left with 2 challenges:
- convert our SPSS date values in Python datetime objects and
- apply the Python isocalendar to an SPSS data file.
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 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
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
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
- applying regular expressions to SPSS string variables;
- sorting values within cases;
- applying some normalizing transformations that aren't present in SPSS (asinh, third power root).
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.
THIS TUTORIAL HAS 6 COMMENTS:
By Ruben Geert van den Berg on April 20th, 2023
Hi Iosif!
Did you run the block right before it?
begin program python3.
import datetime
from spssdata import CvtSpssDatetime
def iso(spssdate):
return CvtSpssDatetime(spssdate).isocalendar()
end program.
What happens if you do so?
If you're on a very old SPSS version, you could get an error. In this case, try replacing
begin program python3.
by
begin program.
Hope that helps!