Data editing > Expressions > Logical expressions > Comparing data variables and data constants > Fields of data variables
 
Fields of data variables
Quick reference
To test whether a field contains a given list of codes, type:
var_name(start,end) = $codes$
To test whether a field contains a given list of letters, type:
var_name(start,end) = $letters$
To test whether two fields contain identical strings, type:
var_name1(start1,end1) = var_name2(start2,end2)
To test whether the codes in one field differ from a given string, type:
var_name(start,end)u$codes$
To test whether the codes in one field differ from those in another, type:
var_name1(start1,end1)uvar_name2(start2,end2)
The contents of data fields must be enclosed in dollar signs with each code in the string referring to a separate column in the field. For example, to check whether columns 47 to 50 contain the codes –, 6, 4 and 9 respectively, type:
c(47,50)=$–649$
The only data for which this expression is true is:
+----5----+
  -649
However, if the data is:
+----5----+
  -529
  164&
the expression would be false because all columns are multicoded.
You can also check whether a field contains a given list of letters. For example, to check whether columns 55 to 57 contained the string AAA, type:
c(55,57)=$AAA$
The only data for which this expression is true is:
+----5-----+
AAA
The examples use columns, but the same rules apply to data variables that you define yourself. For example:
rating(1,4)=$1234$
checks whether the field rating1 to rating4 contains the codes 1, 2, 3 and 4 in that order. That is, it checks whether rating1 contains a 1, whether rating2 contains a 2, and so on.
When checking the contents of fields in this way, make sure that you enter as many columns as there are codes in the string (that is, five codes require five columns). The exception to this rule occurs when you are checking for blanks when the expression may be shortened to:
c(50,80)=$ $
This type of statement may also be used to compare two fields, to check whether the second field contains exactly the same codes as the first field. When you compare one field with another, Quantum takes each column in the first field in turn and looks to see whether the corresponding column in the second field contains exactly the same codes. For example, if the first column of the first field contains a code 1 and a code 2 and nothing else, then Quantum checks whether the first column of the second field also contains a code 1 and a code 2 and nothing else. If all columns of the second field are identical to their counterparts in the first field, then the expression is true; otherwise it is false. Here is an example:
c(129,132)=c(356,359)
For this expression to be true, column 129 must contain exactly the same codes as column 356, column 130 must be exactly the same as column 357, and so on. Once again, the two expressions on either side of the equals sign must be the same length.
Notes
Comparisons of one data variable against another are concerned with columns and codes: they are not concerned with the arithmetic values of the codes in the fields as a whole.
If you have:
----+----3----+----
02 2
the expression:
c(24,25)=c(34,35)
is false because the string $02$ is not the same as the string $­2$. If you want to compare fields arithmetically (for example, is 02 the same as 2), use the .eq. operator:
c(24,25).eq.c(34,35)
to test whether the value in c(34,35) was equal to the value in c(24,25).
For more information about the .eq. operator, see Comparing values.
To check whether the codes in one field do not match a given string or the codes in another field, you can use the u (unequals) operator:
c(m,n)U$codes$     cmUcn     c(m,n)Uc(m1,n1)
If codes in the field c(m,n) do not match the given string or the codes in c(m1,n1) then the expression is true. If the two fields are identical, then the expression is false.
The comparison is of codes in columns, where the columns are compared on a one to one basis. It is not a comparison of a field with a numeric value, or of the numeric values in two fields. Numeric comparisons for inequality are written with the .ne. operator.
For more information about numeric comparisons, see Comparing values.
Example of the unequals operator
The statement:
c(67,69)u$123$
is true at all times, unless the data reads:
+----7----+
123
The expression:
c(67,69)uc(77,79)
is true as long as columns 67 to 69 differ by at least one code from columns 77 to 79. If the data is:
+----7----+----8
123 256
the expression is true because each of columns 77 to 79 differ from columns 67 to 69. Also, if the data:
+----7----+----8
123 123
5
the expression is true because column 77 is multicoded ‘15’. The only time the expression is false is when columns 67 to 69 are identical to columns 77 to 79.
See also
Comparing data variables and data constants