SPSS Tutorials

BASICS REGRESSION T-TEST ANOVA CORRELATION

Sort Values within Cases

Introduction

Sorting values within cases can be done very easily with the SPSSINC TRANS extension. If many variables are involved, this command may be generated by Python to increase the efficiency even further.

The SPSSINC TRANS Extension

SPSSINC Trans Syntax Example

*1. Create some test data.

data list free/id.
begin data
1 2 3 4 5 6 7 8 9 10
end data.

do repeat n = n1 to n5.
compute n = tru(rv.uni(1,6)).
end repeat.
exe.

*2. Create new variables with sorted values from n1 to n5.

spssinc trans result = s1 to s5 type=0
/formula "sorted([n1,n2,n3,n4,n5])".

Generating the command with Python

The basic syntax for sorting needs to have the variable names spelled out in SPSSINC TRANS. In case many variables are involved, this can be circumvented by specifying variable names using the TO and ALL keywords and have Python create the required syntax for you.

SPSS Python Syntax Example

*In case many variables are involved, the TO keyword can be used like so.

begin program.
inputVars = 'n1 to n5' # Specify input variables here.
outputVars = 's1 to s5' # Specify output variables here.
import spss,spssaux
spss.Submit('''
spssinc trans result = %s type=0
/formula "sorted([%s])".
'''%(outputVars,','.join(spssaux.VariableDict(caseless = True).expand(inputVars))))
end program.

Previous tutorial: Prefix Many Value Labels

Next tutorial: Find Within Subjects Favorite over Several Variables

Let me know what you think!

*Required field. Your comment will show up after approval from a moderator.

This tutorial has 1 comment!