Examples (DO REPEAT-END REPEAT command)

Creating Multiple New Variables with the Same Value

DO REPEAT R=REGION1 TO REGION5.
COMPUTE R=0.
END REPEAT.
  • DO REPEAT defines the stand-in variable R, which represents five new numeric variables: REGION1, REGION2, REGION3, REGION4, and REGION5.
  • The five variables are initialized to 0 by a single COMPUTE specification that is repeated for each variable on the replacement list. Thus, the program generates five COMPUTE commands from the one specified.
  • Stand-in variable R ceases to exist once control passes out of the DO REPEAT structure.

Multiple Replacement Lists

DO REPEAT existVar=firstVar TO var5
         /newVar=new1 TO new5
         /value=1 TO 5.
COMPUTE newVar=existVar*value.
END REPEAT PRINT.
 
****generated COMPUTE commands****
  57 +COMPUTE        new1=firstVar*1
  58 +COMPUTE        new2=secondVar*2
  59 +COMPUTE        new3=var3*3
  60 +COMPUTE        new4=fourthVar*4
  61 +COMPUTE        new5=var5*5.
  • existVar=firstVar to var5 includes all existing variables from firstVar to var5, in file order.
  • newVar=new1 TO new5 specifies five new variables: var1, var2, var3, var4, and var5.
  • value=1 to 5 specifies a list of five consecutive values: 1, 2, 3, 4, 5.
  • All three replacement lists contain five items, and five COMPUTE commands are generated.

Generating Data with DO REPEAT, LOOP, and INPUT PROGRAM

* This example shows a typical application of INPUT PROGRAM, LOOP,
   and DO REPEAT. A data file containing random numbers is generated.
 
INPUT PROGRAM.
+  LOOP #I = 1 TO 1000.
+     DO REPEAT RESPONSE = R1 TO R400.
+          COMPUTE RESPONSE = UNIFORM(1) > 0.5.
+     END REPEAT.                    
+     COMPUTE AVG = MEAN(R1 TO R400).
+     END CASE.                        
+   END LOOP.
+   END FILE.
END INPUT PROGRAM.

FREQUENCIES VARIABLE=AVG
 /FORMAT=CONDENSE
 /HISTOGRAM
 /STATISTICS=MEAN MEDIAN MODE STDDEV MIN MAX.
  • The INPUT PROGRAM—END INPUT PROGRAM structure encloses an input program that builds cases from transformation commands.
  • The indexing variable (#I) on LOOP—END LOOP indicates that the loop should be executed 1000 times.
  • The DO REPEAT—END REPEAT structure generates 400 variables, each with a 50% chance of being 0 and a 50% chance of being 1. This is accomplished by specifying a logical expression on COMPUTE that compares the values returned by UNIFORM(1) to the value 0.5. (UNIFORM(1) generates random numbers between 0 and 1.) Logical expressions are evaluated as false (0), true (1), or missing. Thus, each random number returned by UNIFORM that is 0.5 or less is evaluated as false and assigned the value 0, and each random number returned by UNIFORM that is greater than 0.5 is evaluated as true and assigned the value 1.
  • The second COMPUTE creates variable AVG, which is the mean of R1 to R400 for each case.
  • END CASE builds a case with the variables created within each loop. Thus, the loop structure creates 1000 cases, each with 401 variables (R1 to R400, and AVG).
  • END FILE signals the end of the data file generated by the input program. If END FILE were not specified in this example, the input program would go into an infinite loop. No dataset would be built, and the program would display an error message for every procedure that follows the input program.
  • FREQUENCIES produces a condensed frequency table, histogram, and statistics for AVG. The histogram for AVG shows a normal distribution.