Bit masks are used in bit testing to compare internal
bits in a value's representation. You can perform bit testing on both
character and numeric variables. The general form of the operation
is:
expression comparison-operator
bit-mask
The following are the
components of the bit-testing operation:
can be any valid SAS
expression. Both character and numeric variables can be bit tested.
When SAS tests a character value, it aligns the left-most bit of the
mask with the left-most bit of the string; the test proceeds through
the corresponding bits, moving to the right. When SAS tests a numeric
value, the value is truncated from a floating-point number to a 32-bit
integer. The right-most bit of the mask is aligned with the right-most
bit of the number, and the test proceeds through the corresponding
bits, moving to the left.
compares an expression
with the bit mask. Refer to
Comparison Operators for a discussion of these operators.
is a string of 0s,
1s, and periods in quotation marks that is immediately followed by
a B. Zeros test whether the bit is off; ones test whether the bit
is on; and periods ignore the bit. Commas and blanks can be inserted
in the bit mask for readability without affecting its meaning.
CAUTION:
Truncation
can occur when SAS uses a bit mask.
If the expression is
longer than the bit mask, SAS truncates the expression before it compares
it with the bit mask. A false comparison might result. An expression's
length (in bits) must be less than or equal to the length of the bit
mask. If the bit mask is longer than a character expression, SAS prints
a warning in the log, stating that the bit mask is truncated on the
left, and continues processing.
The following example
tests a character variable:
if a='..1.0000'b then do;
If the third bit of
A (counting from the left) is on, and the fifth through eighth bits
are off, the comparison is true and the expression result is 1. Otherwise,
the comparison is false and the expression result is 0. The following
is a more detailed example:
data test;
input @88 bits $char1.;
if bits='10000000'b
then category='a';
else if bits='01000000'b
then category='b';
else if bits='00100000'b
then category='c';
run;
Note: Bit masks cannot be used
as bit literals in assignment statements. For example, the following
statement is not valid:
x='0101'b; /* incorrect*/
The $BINARY
w. and BINARY
w. formats and the $BINARY
w., BINARY
w.
d, and BITS
w.
d informats can be useful
for bit testing. You can use them to convert character and numeric
values to their binary values, and vice versa, and to extract specified
bits from input data. See
SAS Formats and Informats: Reference for complete descriptions of these formats and informats.