3

I have an sav file with plenty of variables. What I would like to do now is create macros/routines that detect basic properties of a range of item sets, using SPSS syntax.

COMPUTE scale_vars_01 = v_28 TO v_240.

The code above is intended to define a range of items which I would like to observe in further detail. How can I get the number of elements in the "array" scale_vars_01, as an integer?

Thanks for info. (as you see, the SPSS syntax is still kind of strange to me and I am thinking about using Python instead, but that might be too much overhead for my relatively simple purposes).

nilsole
  • 1,663
  • 2
  • 12
  • 28

2 Answers2

2

One way is to use COUNT, such as:

COUNT Total = v_28 TO v_240 (LO THRU HI).

This will count all of the valid values in the vector. This will not work if the vector contains mixed types (e.g. string and numeric) or if the vector has missing values. An inefficient way to get the entire count using DO REPEAT is below:

DO IF $casenum = 1.
COMPUTE Total = 0.
DO REPEAT V = v_28 TO V240.
  COMPUTE Total = Total + 1.
END REPEAT.
ELSE.
  COMPUTE Total = LAG(Total).
END IF.

This will work for mixed type variables, and will count fields with missing values. (The DO IF would work the same for COUNT, this forces a data pass, but for large datasets and large lists will only evaluate for the first case.)

Python is probably the most efficient way to do this though - and I see no reason not to use it if you are familiar with it.

BEGIN PROGRAM.
import spss
beg = 'X1'
end = 'X10'

MyVars = []
for i in xrange(spss.GetVariableCount()):
  x = spss.GetVariableName(i)
  MyVars.append(x)

len = MyVars.index(end) - MyVars.index(beg) + 1
print len 
END PROGRAM.
Andy W
  • 5,031
  • 3
  • 25
  • 51
2

Statistics has a built-in macro facility that could be used to define sets of variables, but the Python apis provide much more powerful ways to access and use the metadata. And there is an extension command SPSSINC SELECT VARIABLES that can define macros based on variable metadata such as patterns in names, measurement level, type, and other properties. It generates a macro listing these variables that can then be used in standard syntax.

JKP
  • 5,419
  • 13
  • 5