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 FILEcommand to control whether split-file groups will be displayed in the same table or in separate tables. TheSPLIT FILEcommand should be called before theStartProcedurefunction. - The IsEndSplit method from the
Cursorclass 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.Submitfunction is used to submit command syntax to create a split on a gender variable. TheLAYEREDsubcommand on theSPLIT FILEcommand indicates that results for different split groups are to be displayed in the same table. Notice that the command syntax is executed before callingspss.StartProcedure. - The
spss.SplitChangefunction 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
Cursorclass. Once a split change is detected, the pivot table is populated with the results from the previous split. - The value returned from the
fetchonemethod is None at a split boundary. Once a split has been detected, you will need to callfetchoneagain to retrieve the first case of the new split group, followed byspss.SplitChange. Note:IsEndSplitreturns 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 fromIsEndSplit, the end of the dataset is identified by a return value of None from a subsequent call tofetchone, as shown in this example.