Comparison Operators: <, <=, >, >=, =, ^= |
Comparison operators compare two matrices element by element and compute a new matrix that contains only zeros and ones. If an element comparison is true, the corresponding element of the new matrix is 1. If the comparison is not true, the corresponding element is 0. Unlike in the SAS DATA step, the SAS/IML language does not accept the English equivalents GT and LT for the greater than and less than operators.
For example, the following statements assign the matrix c, shown in Figure 23.2:
a = {1 7 3, 6 2 4}; b = {0 8 2, 4 1 3}; c = a>b; print c;
c | ||
---|---|---|
1 | 0 | 1 |
1 | 1 | 1 |
You can also use the comparison operators to conveniently compare all elements of a matrix with a scalar.
If either argument is a scalar, then an elementwise comparison is performed between each element of the matrix and the scalar.
You can also compare an matrix with a row or column vector.
If the comparison is with an column vector, each row of the vector is compared to each row of the matrix.
If the comparison is with a row vector, each column of the vector is compared to each column of the matrix.
For example, the following statements assign the matrix d, shown in Figure 23.3:
d = (a>=4); /* the parentheses are not necessary */ print d;
d | ||
---|---|---|
0 | 1 | 0 |
1 | 0 | 1 |
When you are making conditional comparisons, all values of the result must be nonzero for the condition to be evaluated as true, as shown in the following statements:
if a>=b then do; /* more statements */ end;
The previous DO block is executed only if every element of a is greater than or equal to the corresponding element in b. For the a and b matrices defined in this section, the DO block is not executed. See the descriptions of the ALL and ANY functions.
If a numeric missing value occurs in a matrix, the inequality comparison operators treat it as a value that is less than any valid nonmissing value.
You can compare elements of a character matrix. Character values are compared in ASCII order. In ASCII order, numerals precede uppercase letters, which precede lowercase letters. If the element lengths of two character matrices are different, the shorter elements are padded on the right with blanks for the comparison.