insert Method (Python)

.insert(name,type,index). Inserts a new variable into the associated dataset and inserts a corresponding Variable object into the associated VariableList instance. The argument name specifies the variable name. The optional argument type specifies the variable type--numeric or string. If type is omitted, the variable is numeric. The optional argument index specifies the position for the inserted variable and Variable object (the first position has an index value of 0) and can take on the values 0,1,...,n where n is the number of variables in the dataset. If index is omitted or equal to n, the variable is appended to the end of the list.

  • 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).
  • The properties of the new variable are set using the Variable object created by the insert method. See the topic Variable Class (Python) for more information.

Example

DATA LIST FREE/var1 (F2) var3 (A1).
BEGIN DATA.
1 a
END DATA.
BEGIN PROGRAM.
import spss
spss.StartDataStep()
datasetObj = spss.Dataset()
# Insert a numeric variable at index position 1 in the active dataset
datasetObj.varlist.insert('var2',0,1)
spss.EndDataStep()
END PROGRAM.