SetVarNameAndType Method (Python)

.SetVarNameAndType(varName,varType). Creates one or more new variables in the active dataset. The argument varName is a list or tuple of strings that specifies the name of each new variable. The argument varType is a list or tuple of integers specifying the variable type of each variable named in varName. varName and varType must be the same length. For creating a single variable you can also use the SetOneVarNameAndType method.

  • This method is only available in write mode.
  • Numeric variables are specified by a value of 0 for the variable type. String variables are specified with a type equal to the defined length of the string (maximum of 32767).

Example

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.