Jon Peck made some great comments on my prior post on passing arguments to the SPSSINC TRANS function. Besides advice on that I should be quoting the argument on the FORMULA statement, he gave examples of how you can use the “TO” argument in both passing variables lists within the python formula and assigning variables to the results. Here is a brief example of their use.
First I will be working with a tiny, toy dataset:
DATA LIST FREE / X1 TO X4. BEGIN DATA 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 END DATA. DATASET NAME Test.
Now here is a command that returns the second lowest value in a list. (While there are plenty of things you can do in base code, this python code is very simple compared to what you would have to do in vanilla SPSS to figure this out.) In a nutshell you can specify the variable list on the
/VARIABLE subcommand (and mix in
TO to specify adjacent variables as in most SPSS commands). And then insert these into the python formula by specifying
SPSSINC TRANS RESULT = Second /VARIABLES X1 TO X4 /FORMULA "sorted([<>])".
In my prior post, I showed how you could do this for the original variables, which would look like
/FORMULA "sorted([X1,X2,X3,X4])". Here you can see I’ve specified a set of variables on the VARIABLES subcommand, and inserted them into a list using
<> in brackets produces a list in python. I then sort the list and grab the second element (located at 1, since python uses 0 based indices). You can also mix variables in the dataset and the
<> listed on the variables subcommand. See here for an example.
You can also use the
TO modifier in making a new set of variables. Here I return the sorted variables
X1 TO X4 as a new set of variables
S1 TO S4.
SPSSINC TRANS RESULT = S1 TO S4 /VARIABLES X1 TO X4 /FORMULA "sorted([<>])".
In both the prior examples I omitted the
TYPE argument, as it defaults to 0 (i.e. a numeric variable returned as a float). But when specifying variable lists of the same type for multiple variables you can simply specify the type one time and the rest of the results are intelligently returned as the same. Here is the same sorted example, except that I return the results each as a string of length 1 as opposed to a numeric value.
SPSSINC TRANS RESULT = A1 TO A4 TYPE = 1 /VARIABLES X1 TO X4 /FORMULA "map(str, sorted([<>]))".