You can use the
transform command to quickly access and modify your survey data with Python code.
Click here for information on modifying data using external data files.
1: Prepare a Python File
To modify your survey data using transform, you will first need to create a Python file (e.g.,
somefile.py) containing your transformation code. This code can include variable, marker, and status modifications, as well as instructions for removing or replacing an entire record.
1.1: Modifying Variables
When modifying variables, include the variable label names, original data values (i.e., what will appear in the data file), and the values you would like assigned to them.
For example, assuming you have the variables
q2r2, you can add the following code to your file to assign a value of "
q1 has a value of "
if q1 == 1: q2r1 = q2r2.val
Keep in mind that a single question may have multiple variables assigned to it. Those variables are grouped in "variable groups", and you can get all the variables for a question by accessing its
For example, if you had a one-dimensional check-box question called "q1", with
r3 variables, accessing
q.q1 would give you a list of these three (which you could then check for certain values or modify).
1.2: Modifying Markers
To modify survey markers using Python, run one of the following commands, specifying the marker you would like to modify:
hasMarker("..."): will check for records with the specified marker
setMarker: will set a marker for the specified record
removeMarker: will remove the specified marker
1.3: Modifying Status
You can modify the status of a respondent using the status variable. For example, the code below would change the completion status to terminated for all respondents with q3r2 set to "1":
if q3r2 == 1: status = UNQUALIFIED
1.4: Deleting a Record
To delete a record, simply call the
if q3r3 == 1: delete()
1.5: Replacing a Record
replace function allows you to specify the default values for variables using the variable labels and the new values.
For example, if you have variables
q1r2 and would like to replace their values with "
0 " and "
1 ", respectively, you could use the following command:
replace([q1r1, q1r2], 0, 1)
2: Run the
Next, run the
transform command, specifying your Python file:
transform survey somefile.py
If desired, you can include one of the following attributes to further define your command:
-v: will trace changes as they happen
-t: will run your transformation on test data
-a: will run your transformation while allowing modification of hidden variables (
Note: If your code transformation is simple, you can alternatively pass it through on the command line by adding a space to the command (the extra space will designate a command code rather than a file). For example,
transform.'setMarker(“qualified”)' would set the qualified marker on every respondent in the current survey.
Once run, you will be given a summary of the changes and prompted to continue before those changes are saved. The code in your Python file is run on each row of the survey data, and works similarly to variables in a tab-delimited file: each variable in the data corresponds to one Python variable.
3: Additional Considerations
- Similar to the TabImport script,
transformmakes a backup to the
- If using the
-tswitch, you can also use a script to create your own customized output file. For example:
transform -t . 'print "uuid = %s, status = %s" % (uuid, status)'