VECTOR outside a Loop Structure (VECTOR command)

VECTOR is most commonly associated with the loop structure, since the index variable for LOOP can be used as the subscript. However, the subscript can come from elsewhere, including from the data.

Example

* Create a single case for each of students 1, 2, and 3.
 
DATA LIST /STUDENT 1 SCORE 3-4 TESTNUM 6.
BEGIN DATA
1 10 1
1 20 2
1 30 3
1 40 4
2 15 2
2 25 3
3 40 1
3 55 3
3 60 4
END DATA.
 
VECTOR RESULT(4).
COMPUTE RESULT(TESTNUM)=SCORE.
 
AGGREGATE OUTFILE=*/BREAK=STUDENT
        /RESULT1 TO RESULT4=MAX(RESULT1 TO RESULT4).
 
PRINT FORMATS RESULT1 TO RESULT4 (F2.0).
PRINT /STUDENT RESULT1 TO RESULT4.
EXECUTE.
  • Data are scores on tests recorded in separate cases along with a student identification number and a test number. In this example, there are four possible tests for three students. Not all students took every test.
  • The vector RESULT creates the variables RESULT1 through RESULT4.
  • For each case, COMPUTE assigns the SCORE value to one of the four vector variables, depending on the value of TESTNUM. The other three vector variables for each case keep the system-missing value they were initialized to.
  • Aggregating by the variable STUDENT creates new cases, as shown by the output from the PRINT command. The MAX function in AGGREGATE returns the maximum value across cases with the same value for STUDENT. If a student has taken a particular test, the one valid value is returned as the value for the variable RESULT1, RESULT2, RESULT3, or RESULT4.
Figure 1. PRINT output after aggregating
PRINT output after aggregating