Write Mode (Python)
This mode is used to add new variables, along with their case values,
to an existing dataset. It cannot be used to append cases to the active
dataset. Write mode is specified with spss.Cursor(accessType='w')
.
- All of the methods available in read mode are also available in write mode.
- When adding new variables, the
CommitDictionary
method must be called after the statements defining the new variables and prior to setting case values for those variables. You cannot add new variables to an empty dataset. - When setting case values for new variables, the
CommitCase
method must be called for each case that is modified. Thefetchone
method is used to advance the record pointer by one case, or you can use thefetchmany
method to advance the record pointer by a specified number of cases.Note: If a case filter (specified with theFILTER
orUSE
command) is in effect,fetchone
andfetchmany
advance the record pointer through the set of cases that have not been filtered out. - Changes to the active dataset do not take effect until the cursor is closed.
- Write mode supports multiple data passes and allows you to add
new variables on each pass. In the case of multiple data passes where
you need to add variables on a data pass other than the first, you
must call the
AllocNewVarsBuffer
method to allocate the buffer size for the new variables. When used,AllocNewVarsBuffer
must be called before reading any data withfetchone
,fetchmany
, orfetchall
. - The
Cursor
methods SetVarNameAndType and SetOneVarNameAndType are used to add new variables to the active dataset, and the methods SetValueChar and SetValueNumeric are used to set case values.
Example
In this example, we create a new numeric variable and a new string variable and set their values for all cases.
DATA LIST FREE /var1 (F) var2 (A2) var3 (F).
BEGIN DATA
11 ab 13
21 cd 23
31 ef 33
END DATA.
BEGIN PROGRAM.
import spss
cur=spss.Cursor(accessType='w')
cur.SetVarNameAndType(['var4','strvar'],[0,8])
cur.SetVarFormat('var4',5,2,0)
cur.CommitDictionary()
for i in range(cur.GetCaseCount()):
cur.fetchone()
cur.SetValueNumeric('var4',4+10*(i+1))
cur.SetValueChar('strvar','row' + str(i+1))
cur.CommitCase()
cur.close()
END PROGRAM.
- An instance of the
Cursor
class in write mode is created and assigned to the variable cur. - The SetVarNameAndType method is used to add two new variables to the active dataset. var4 is a numeric variable and strvar is a string variable of width 8.
- SetVarFormat sets the display format for var4. The integers 5, 2, and 0 specify the format type (5 is standard numeric), the defined width, and the number of decimal digits respectively.
- The
CommitDictionary
method is called to commit the new variables to the cursor before populating their case values. - The
SetValueNumeric
andSetValueChar
methods are used to set the case values of the new variables. TheCommitCase
method is called to commit the changes for each modified case. -
fetchone
advances the record pointer to the next case.
Example: Setting Values for Specific Cases
In this example, we create new variables and set their values for
specific cases. The fetchone
method is used to advance
the record pointer to the desired cases.
DATA LIST FREE /code (A1) loc (A3) emp (F) dtop (F) ltop (F).
BEGIN DATA
H NY 151 127 24
W CHI 17 4 0
S CHI 9 3 6
W ATL 12 3 0
W SDG 13 4 0
S ATL 10 3 7
S SDG 11 3 8
END DATA.
BEGIN PROGRAM.
import spss
cur=spss.Cursor(accessType='w')
cur.SetVarNameAndType(['emp_est','dtop_est','ltop_est'],[0,0,0])
cur.SetVarFormat('emp_est',5,2,0)
cur.SetVarFormat('dtop_est',5,2,0)
cur.SetVarFormat('ltop_est',5,2,0)
cur.CommitDictionary()
for i in range(cur.GetCaseCount()):
row=cur.fetchone()
if (row[0].lower()=='s'):
cur.SetValueNumeric('emp_est',1.2*row[2])
cur.SetValueNumeric('dtop_est',1.2*row[3])
cur.SetValueNumeric('ltop_est',1.2*row[4])
cur.CommitCase()
cur.close()
END PROGRAM.
Example: Multiple Data Passes
In this example, we read the data, calculate a summary statistic, and use a second data pass to add a summary variable to the active dataset.
DATA LIST FREE /var (F).
BEGIN DATA
57000
40200
21450
21900
END DATA.
BEGIN PROGRAM.
import spss
cur=spss.Cursor(accessType='w')
cur.AllocNewVarsBuffer(8)
total=0
for i in range(spss.GetCaseCount()):
total+=cur.fetchone()[0]
meanVal=total/spss.GetCaseCount()
cur.reset()
cur.SetOneVarNameAndType('mean',0)
cur.CommitDictionary()
for i in range(spss.GetCaseCount()):
row=cur.fetchone()
cur.SetValueNumeric('mean',meanVal)
cur.CommitCase()
cur.close()
END PROGRAM.
- Because we will be adding a new variable on the second data pass,
the
AllocNewVarsBuffer
method is called to allocate the required space. In the current example, we are creating a single numeric variable, which requires eight bytes. - The first
for
loop is used to read the data and total the case values. - After the data pass, the
reset
method must be called prior to defining new variables. - The second data pass (second
for
loop) is used to add the mean value of the data as a new variable.