The simplest form of arithmetic expression is a single positive or negative number such as 10 or -26.5 or an integer or real variable.
Although the C array is data, columns may also be used in arithmetic when the response coded into those columns is a numeric response, such as a respondent’s age or the number of different shops he or she visited. For example, if columns 243 to 247 contain the codes 4,7,2,6 and 0 respectively the value in c(243,247) could be read as 47,260. Similarly, if columns 45 to 48 contain 7, 8, a dot and 2 respectively, the value in cx(45,48) would be 78.2.
Blank columns in a field are ignored when the codes in those columns are evaluated. Thus, if columns 20 to 21 contain the codes 6 and 7 respectively, and column 22 is blank, the codes in c(20,22) will be evaluated as 67. A similar result is produced if the blank column appears anywhere else in the field. All the examples of c(20,22) below produce an arithmetic value of 67:
+----2----+ +----2----+ +----2----+
67 67 6 7
The same applies to multicoded columns. If you use a multicoded column as part of an arithmetic expression, the multicoded column will be ignored. The exception to this is a multicode of a digit and a minus sign which creates a negative number: a minus sign anywhere in a numeric field negates the value in the field as a whole, not just the number it is multicoded with. For example:
----+----1----+----2
5 3778 is 5378 9
0
2---+----3----+----4
12-4 is -1234 3
4---+----5----+----6
83- is -83
Combining arithmetic expressions
Quick Reference
To combine arithmetic expressions, type:
variable operator variable [operator variable ... ]
where variable is a numeric value or the name of a variable containing a numeric value, and operator is one of the arithmetic operators +, −, * (multiply) or / (divide).
More often than not, you will want to combine numeric expressions to form a larger expression, for instance to count the number of records read with a given code in a named column.
Arithmetic expressions are linked with any of the arithmetic operators listed below:
Expressions may contain more than one of these operators, for instance:
t5 + c(134,136) / otot c(150,152) * 10 + 2.5
Quantum evaluates such expressions in the following order:
1. Expressions in parentheses 2. Multiplication and division 3. Addition and subtraction
If you wish to change this order you should enclose the expressions which go together in parentheses. The first expression in the example above will be evaluated by dividing the value in columns 134 to 136 by otot and adding the result to t5. If you change the expression to:
(t5 + c(134,136)) / otot
this adds the values of t5 and c(134,136) first and then divides that by otot. Let’s substitute numbers and compare the results. If t5=10, otot=5 and the value in c(134,136) is 125, the two versions of the expression would read as follows:
10 + 125 / 5 = 35 and (10 + 125) / 5 = 27 + (addition) * (multiplication)
− (subtraction) / (division)
Where two integer expressions are combined, the result is integer (any decimal places are ignored), but if an expression contains a real then the result will be real. Therefore, if t1=5 and t2=3, then:
If you use parentheses in expressions which contain both integer and real variables, you need to take extra care to ensure that your expression is producing the correct results. Let’s look at an example to illustrate how an expression can look correct but can still produce unexpected results.
If we assume that t40=2 and t41=70, the expression:
t40 * 100.0 / t41
yields a result of 2.85714 (that is, 200.0/70). The final value will be 2.85714 if the result is saved in a real variable, or 2 if it is saved in an integer variable.
If we use parentheses:
(t40 / t41) * 100.0
the result is 0.0 (or 0 if saved in an integer variable). The reason for this is as follows. Because Quantum evaluates expressions in parentheses before it deals with the rest of the expression, it treats that expression as integer arithmetic. The rules for integer arithmetic dictate that real results are truncated at the decimal point, so the true result of 0.0285714 becomes 0. Any multiplication involving zero is always zero, so the final result is zero.
If you find that a run gives unexpected zero results, try looking for expressions of this type and checking whether the parenthesized part of the expression has been truncated because the integer division results in a decimal number.
t1 + 4 = 9
t1 + 4.0 = 9.0
t1 * t2 = 15
t1 / t2 = 1
t1 * 1.0 = 5.0
t1 * 1.0 / t2 = 1.66667
Counting the number of codes in a column
Quick Reference
To count the number of codes in a column or list of columns, type:
numb(cn1[’codes’], cn2[’codes’], ... )
If any columns are followed by a code reference, only those codes will be counted for those columns.
The function numb is an arithmetic expression which counts the number of codes in a column or list of columns. Its format is:
numb(cn1,cn2, ... cnn)
where cn1 to cnn are the columns whose codes are to be counted. So, if we wanted to count the number of codes in columns 132 to 135 we would type:
numb(c132,c133,c134,c135)
Notice that even though the columns are consecutive, each one is entered separately, with each column number preceded by a ‘c’. It is incorrect to define only the start and end columns of a field when using numb. Therefore it is wrong to write numb(c(132,135)) or numb(c(132,135)) and, if you write statements such as these, Quantum will flag them as errors.
Sometimes you will only be interested in certain codes, for instance you may want to know how many 1, 2 or 3 codes there are in a group of columns. In this case the function is entered as:
numb(cn’p1’,cn’p2’, ... cnn’pn’)
where p1 to pn are the codes to be counted. Only the named codes are counted — any others appearing in the columns are ignored. Let’s say our data on card 1 is as follows:
1---+----2---...---5----+----4 1 2 1 6 / / 8 6 7 9
and we want to count the number of codes in column 115 and also the number of codes in the range
‘5/8’ in columns 121 and 157. The expression would be entered as:
numb(c115,c121’5/8’,c157’5/8’)
When Quantum checks these columns and codes, it will tell us that there are 9 codes in these columns which are within the given ranges. These codes are all four codes in column 115 (we did not specify which codes to count in that column), codes 5 and 6 in column 121 (codes 2 to 4 are outside the given range), and codes 5 to 7 in column 157 (codes 1 to 4 are outside the given range).
Generating a random number
Quick Reference
To generate a random number in the range 1 to n, type:
random(n) in the edit section.
Quantum can generate random numbers automatically with the random function:
random(n)
where n is the maximum value the random number may take. So, to generate a random number in the range 1 to 100, the expression would read:
random(100)
The number produced may be saved for later use in an integer variable or column, thus:
rnum=random(32)
c(110,112)=random(156)
When using random with columns, always make sure that the number of columns allocated to the number is sufficient to store the highest possible number that can be generated. In our example, we need three columns in order to store numbers up to 156.
✎
random generates a different random value each time it is run, even on reruns of the same job.If you want to retain the same set of random values between runs, copy them into the data the first time you run the job.