Question

I've got 53 variables named W1_C14_0 to W1_C14_52, and each has a value from 1 to 15. I need to find how many "spells" of each number there are in this list - i.e. how many seperate runs of 1's, 2's etc in each case. This is what I'm doing and it's working fine, but is there any way to condense it into a loop?

DO REPEAT first = W1_C14_0 to W1_C14_51 /
second = W1_C14_1 to W1_C14_52 .
   DO IF (SYSMIS(first) OR first<>second) .
      DO IF (second=1) . 
         COMPUTE W1_spells1 = W1_spells1 + 1 .
      ELSE IF (second=2) .
         COMPUTE W1_spells2 = W1_spells2 + 1 .
      ELSE IF (second=3) .
         COMPUTE W1_spells3 = W1_spells3 + 1 .

      *and so on down to...

      ELSE IF (second=15) .
         COMPUTE W1_spells15 = W1_spells15 + 1 .
      END IF.
   END IF.
END REPEAT .

No correct solution

OTHER TIPS

You can loop through the spell variables using the VECTOR command.

VECTOR W1_spells = W1_spells1 TO W1_spells15.

DO REPEAT first = W1_C14_0 to W1_C14_51 /
second = W1_C14_1 to W1_C14_52 .
   DO IF (SYSMIS(first) OR first<>second).
      LOOP #i=1 TO 15.
         DO IF (second=#i) . 
            COMPUTE W1_spells(#i) = W1_spells(#i) + 1.
         END IF.
      END LOOP.
   END IF.
END REPEAT.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top