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).

## 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.

*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

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 Transform 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

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.

# THIS TUTORIAL HAS 6 COMMENTS:

• ### By Jon Peck on April 19th, 2023

Thanks, Rubin. The SPSSINC TRANS command does really open the door to all sorts of interesting transformations.

Weeks, annoyingly, do not fit smoothly into years. As reported in this link
https://www.timeanddate.com/date/week-numbers.html
there are at least six different week numbering systems in use around the world. ISO 8601 is probably the best compromise, but it is not the standard in the US and a number of other countries. Week numbering

• ### By Jon K Peck on April 19th, 2023

... continuing
Week numbering even differs by discipline.
To add a little to the iso mechanism,
the first week of an ISO year is the first (Gregorian) calendar week of a year containing a Thursday.

• ### By Ruben Geert van den Berg on April 19th, 2023

Hi Jon!

Yes, time standard are pretty messed up, fully agree on that.

The craziest thing are perhaps daylight savings. These mean that some time points occur twice: there was a burglary at 2:30 AM and then at 3 AM the clock is set back to 2 AM so 2:30 AM is going to happen again!

Anyway. SPSS' default week numbers (sometimes resulting in single-day weeks) don't agree to any common standard, do they?

Couldn't you very simply solve the issue with US/Canadian weeks by subtracting a single day LAG to isoweeks/years?

• ### By Iosif Ionel on April 19th, 2023

There is a problem for me. Applying this function

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

The specified function or class was given without a module name
and is not a built-in function: iso

Can you explain this?

Thank you

• ### By Jon K Peck on April 19th, 2023

No matter what definition you use - and the SPSS week number definition is sometimes used, you are going to have some leftover days, which can be a problem for modeling, especially for time series analysis.

If you have daily data, you might lump the short-week days into an adjacent week, but if you need to take into account business days rather than every day, you have even more problems as business days per week will vary.

The extendedTransforms module installed with Statistics has a function that helps with this.

countDaysWExclusions(object):

"""Count days between start and end dates that are not in excluded days and dates