spss.SplitChange Function (Python)

spss.SplitChange(outputName). Used to process splits when creating pivot tables from data that have splits. The argument outputName is the name associated with the output, as specified on the associated call to the StartProcedure function. See the topic spss.StartProcedure Function (Python) for more information.

  • This function should be called after detecting a split and reading the first case of the new split. It should also be called after reading the first case in the active dataset.
  • The creation of pivot table output does not support operations involving data in different split groups. When working with splits, each split should be treated as a separate set of data.
  • Use the SPLIT FILE command to control whether split-file groups will be displayed in the same table or in separate tables. The SPLIT FILE command should be called before the StartProcedure function.
  • The IsEndSplit method from the Cursor class is used to detect a split change.

Example

In this example, a split is created and separate averages are calculated for the split groups. Results for different split groups are shown in a single pivot table. In order to understand the example, you will need to be familiar with creating pivot tables using the BasePivotTable class and creating output with the spss.StartProcedure function.

import spss
from spss import CellText
from spss import FormatSpec

spss.Submit(r"""
GET FILE="/examples/data/employee data.sav".
SORT CASES BY GENDER.
SPLIT FILE LAYERED BY GENDER.
""")

spss.StartProcedure("spss.com.demo")

table = spss.BasePivotTable("Table Title","OMS table subtype")
table.Append(spss.Dimension.Place.row,"Minority Classification")
table.Append(spss.Dimension.Place.column,"coldim",hideName=True)
 
cur=spss.Cursor()
salary = 0; salarym = 0; n = 0; m = 0
minorityIndex = 9
salaryIndex = 5

row = cur.fetchone()
spss.SplitChange("spss.com.demo")
while True:  
   if cur.IsEndSplit():
      if n>0:
         salary=salary/n
      if m>0:
         salarym=salarym/m
      # Populate the pivot table with values for the previous split group
      table[(CellText.String("No"),CellText.String("Average Salary"))] = \ 
             CellText.Number(salary,FormatSpec.Count)
      table[(CellText.String("Yes"),CellText.String("Average Salary"))] = \ 
             CellText.Number(salarym,FormatSpec.Count)
      salary=0; salarym=0; n = 0; m = 0
      # Try to fetch the first case of the next split group
      row=cur.fetchone()
      if not None==row:
         spss.SplitChange("spss.com.demo")
      else:
         #There are no more cases, so quit
         break
   if row[minorityIndex]==1:
      salarym += row[salaryIndex]
      m += 1
   elif row[minorityIndex]==0:
      salary += row[salaryIndex]
      n += 1
   row=cur.fetchone()

cur.close()
spss.EndProcedure()
  • The spss.Submit function is used to submit command syntax to create a split on a gender variable. The LAYERED subcommand on the SPLIT FILE command indicates that results for different split groups are to be displayed in the same table. Notice that the command syntax is executed before calling spss.StartProcedure.
  • The spss.SplitChange function is called after fetching the first case from the active dataset. This is required so that the pivot table output for the first split group is handled correctly.
  • Split changes are detected using the IsEndSplit method from the Cursor class. Once a split change is detected, the pivot table is populated with the results from the previous split.
  • The value returned from the fetchone method is None at a split boundary. Once a split has been detected, you will need to call fetchone again to retrieve the first case of the new split group, followed by spss.SplitChange. Note: IsEndSplit returns True when the end of the dataset has been reached. Although a split boundary and the end of the dataset both result in a return value of True from IsEndSplit, the end of the dataset is identified by a return value of None from a subsequent call to fetchone, as shown in this example.