COMPUTE Examples

 

Example 1: Add two values (Variable data)

 

COMPUTE Q6A_TOTAL = Q6A_1 + Q6A_2

 

This statement adds the value in variable Q6A_1 with the value in variable Q6A_2 and puts the result in variable Q6A_TOTAL.

 

Example 2: Add two values (ASCII data)

 

COMPUTE 3/24:3 = 1/54:2 + 1/57:2

 

This statement adds the value in record 1, columns 54 and 55 with the value in record 1, column 57 and 58 and puts the result in record 3, columns 24 through 26.

 

Example 3: Multiply a value by a constant (Variable data)

 

COMPUTE INCOME = INCOME_1 * 10

 

This multiplies 10 (a constant) by the value in variable INCOME_1 and puts the result in variable INCOME.

 

Example 4: Multiply a value by a constant (ASCII data)

 

COMPUTE 4/22:2 = 2/34 * 10

 

This multiplies 10 (a constant) by the value in record 2, column 34 and puts the result in record 4, columns 22 and 23.

 

Example 5: Using braces to control order (Variable data)

 

COMPUTE Q2_AVG = {Q2A + Q2B} / 2

 

This statement uses braces to control the order in which operations are conducted. The addition of variable Q2A and variable Q2B occurs first, then the resulting number is divided by 2 (a constant). The result is the average of two variables. It is put into Q2_AVG.

 

Example 6: Using braces to control order (ASCII data)

 

COMPUTE 4/22:3 = {2/34 + 2/35} / 2

 

This statement uses braces to control the order in which operations are conducted. The addition of record 2, column 34 and record 2, column 35 occurs first, then the resulting number is divided by 2 (a constant). The result is the average of two variables. It is put into record 4, columns 22 through 24.

 

Example 7: IF logic (Variable data)

 

IF Q10_A (500-999) COMPUTE Q12 = Q10_A / 2

If the value in variable Q10_A is between 500 and 999 it is divided by a constant, 2, with the resulting value being placed in variable Q12.

 

Example 8: IF logic (ASCII data)

 

IF 4/56:3 (500-999) COMPUTE 4/62:5 = 4/56:3 / 2

 

Divides the value in column 56 through 58 of record 4 by 2 (a constant) if the value is between 500 and 999. It puts the resulting value in record 4 columns 62 through 66.

 

Example 9: Adding ten variables (Variable data)

 

COMPUTE AMOUNT_T = AMOUNT_1 A10

 

meaning, ‘Add the next 10 values beginning in variable AMOUNT_1 and store the result in variable AMOUNT_T.’

 

Example 10: Adding ten columns (ASCII data)

 

COMPUTE 4/54:2 = 1/17 A10

 

meaning, ‘Add the next 10 values beginning in record 1, column 17 (columns 17 - 26) and store the result in record 4, columns 54 and 55.’

 

Example 11: With indexed variables

 

 IF 2/19:3 (273) COMPUTE I7 = {I2 + I3} / 2

 

meaning, ‘If record 2 columns 19-21 is a code 273, add the code value in index variables 2 and index variable 3, then divide the result by 2 (a constant) and store in index variable 7.’

 

Related topics:

COMPUTE