If you use a comparison operator, such as the equal sign, to compare a character
variable and a numeric variable, the character variable is converted to numeric.
If you use a numeric variable with an operator that requires a character value, such as
the concatenation operator, the numeric value is converted to character using the
BEST12. format. Because SAS stores the results of the conversion beginning with
the right-most byte, you must store the converted values in a variable of sufficient
length to accommodate the BEST12. format. You can use the LEFT function to left-
justify a result.
If you use a numeric variable on the left side of an assignment statement and a
character variable on the right, the character variable is converted to numeric. In the
opposite situation, where the character variable is on the left and the numeric is on
the right, SAS converts the numeric variable to character using the BESTn. format,
where n is the length of the variable on the left.
When SAS performs an automatic conversion, it prints a note in the SAS log informing
you that the conversion took place. If converting a character variable to numeric
produces invalid numeric values, SAS assigns a missing value to the result, prints an
error message in the log, and sets the value of the automatic variable _ERROR_ to 1.
Note: You can also use the PUT and INPUT functions to convert data values. These
functions can be more efficient than automatic conversion. See “The Concatenation
Operator” on page 107 for an example of the PUT function. See SAS Functions and
CALL Routines: Reference for more details about these functions.
For more information about SAS variables, see Chapter 4, “SAS Variables,” on page 37.
SAS Functions in Expressions
A SAS function is a keyword that you use to perform a specific computation or system
manipulation. Functions return a value, might require one or more arguments, and can be
used in expressions. For further information about SAS functions, see SAS Functions
and CALL Routines: Reference.
SAS Operators in Expressions
Definitions
A SAS operator is a symbol that represents a comparison, arithmetic calculation, or
logical operation; a SAS function; or grouping parentheses. SAS uses two major types of
operators:
prefix operators
infix operators
A prefix operator is an operator that is applied to the variable, constant, function, or
parenthetic expression that immediately follows it. The plus sign (+) and minus sign (−)
can be used as prefix operators. The word NOT and its equivalent symbols are also
prefix operators. The following are examples of prefix operators used with variables,
constants, functions, and parenthetic expressions:
+y
SAS Operators in Expressions 99
-25
-cos(angle1)
+(x*y)
An infix operator applies to the operands on each side of it (for example, 6<8). Infix
operators include the following:
arithmetic
comparison
logical, or Boolean
minimum
maximum
concatenation.
When used to perform arithmetic operations, the plus and minus signs are infix
operators.
SAS also provides several other operators that are used only with certain SAS
statements. The WHERE statement uses a special group of SAS operators, valid only
when used with WHERE expressions. For a discussion of these operators, see Chapter
11, “WHERE-Expression Processing,” on page 177. The _NEW_ operator is used to
create an instance of a DATA step component object. For more information, see Chapter
22, “Using DATA Step Component Objects,” on page 517.
Arithmetic Operators
Arithmetic operators indicate that an arithmetic calculation is performed, as shown in the
following table:
Table 6.3 Arithmetic Operators
Symbol Definition Example Result
** exponentiation
a**3
raise A to the third
power
*
multiplication
*
2*y
multiply 2 by the
value of Y
/ division
var/5
divide the value of
VAR by 5
+ addition
num+3
add 3 to the value of
NUM
- subtraction
sale-discount
subtract the value of
DISCOUNT from the
value of SALE
*
The asterisk (*) is always necessary to indicate multiplication; 2Y and 2(Y) are not valid expressions.
If a missing value is an operand for an arithmetic operator, the result is a missing value.
See Chapter 5, “Missing Values,” on page 83 for a discussion of how to prevent the
propagation of missing values.
100 Chapter 6 Expressions
See “Order of Evaluation in Compound Expressions” on page 108 for the order in
which SAS evaluates these operators.
Comparison Operators
Comparison operators set up a comparison, operation, or calculation with two variables,
constants, or expressions. If the comparison is true, the result is 1. If the comparison is
false, the result is 0.
Comparison operators can be expressed as symbols or with their mnemonic equivalents,
which are shown in the following table:
Table 6.4 Comparison Operators
Symbol
Mnemonic
Equivalent Definition Example
= EQ equal to
a=3
^= NE
not equal to
*
a
ne 3
¬= NE not equal to
~= NE not equal to
> GT greater than
num>5
< LT less than
num<8
>= GE greater than or equal
to
**
sales>=300
<= LE
less than or equal to
***
sales<=100
IN equal to one of a list
num
in (3, 4, 5)
*
The symbol that you use for NE depends on your personal computer.
**
The symbol => is also accepted for compatibility with previous releases of SAS. It is not supported in
WHERE clauses or in PROC SQL.
***
The symbol =< is also accepted for compatibility with previous releases of SAS. It is not supported in
WHERE clauses or in PROC SQL.
See “Order of Evaluation in Compound Expressions” on page 108 for the order in
which SAS evaluates these operators.
You can add a colon (:) modifier to any of the operators to compare only a specified
prefix of a character string. See “Character Comparisons” on page 103 for details.
The IN Operator
You can use the IN operator to compare a value that is produced by an expression on the
left of the operator to a list of values that are given on the right. Individual values can be
SAS Operators in Expressions 101
separated by commas or spaces. You can use a colon to specify a range of sequential
integers.
The three forms of the IN comparison are:
expression IN(value-1<...,value-n>)
expression IN(value-1<... value-n>)
expression IN(value-1<...:value-n>)
The components of the comparison are as follows:
expression
can be any valid SAS expression, but is usually a variable name when it is used with
the IN operator.
value
must be a constant.
For more information and examples of using the IN operator, see “The IN Operator in
Numeric Comparisons” on page 102.
Numeric Comparisons
SAS makes numeric comparisons that are based on values. In the expression A<=B, if A
has the value 4 and B has the value 3, then A<=B has the value 0, or false. If A is 5 and
B is 9, then the expression has the value 1, or true. If A and B each have the value 47,
then the expression is true and has the value 1.
Comparison operators appear frequently in IF-THEN statements, as in this example:
if x<y then c=5;
else c=12;
You can also use comparisons in expressions in assignment statements. For example, the
preceding statements can be recoded as follows:
c=5*(x<y)+12*(x>=y);
Since SAS evaluates quantities inside parentheses before performing any operations, the
expressions (x<y) and (x>=y) are evaluated first, and the result (1 or 0) is substituted
for the expressions in parentheses. Therefore, if X=6 and Y=8, the expression evaluates
as follows:
c=5*(1)+12*(0)
The result of this statement is C=5.
You might get an incorrect result when you compare numeric values of different lengths
because values less than 8 bytes have less precision than those longer than 8 bytes.
Rounding also affects the outcome of numeric comparisons. See Chapter 4, “SAS
Variables,” on page 37 for a complete discussion of numeric precision.
A missing numeric value is smaller than any other numeric value, and missing numeric
values have their own sort order. See Chapter 5, “Missing Values,” on page 83 for more
information.
The IN Operator in Numeric Comparisons
You can use a shorthand notation to specify a range of sequential integers to search. The
range is specified by using the syntax M:N as a value in the list to search, where M is the
lower bound and N is the upper bound. M and N must be integers, and M, N, and all the
102 Chapter 6 Expressions
integers between M and N are included in the range. For example, the following
statements are equivalent.
y = x in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
y = x in (1 2 3 4 5 6 7 8 9 10);
y = x in (1:10);
You can use multiple ranges in the same IN list, and you can use ranges with other
constants in an IN list. The following example shows a range that is used with other
constants to test if X is 0, 1, 2, 3, 4, 5, or 9.
if x in (0,9,1:5);
You can also use the IN operator to search an array of numeric values. For example, the
following code creates an array a, defines a constant x, and then uses the IN operator to
search for
x in array a. Note that the array initialization syntax of array a{10}
(2*1:5) creates an array that contains the initial values of 1, 2, 3, 4, 5, 1, 2, 3, 4, 5.
data _null_;
array a{10} (2*1:5);
x=99;
y = x in a;
put y=;
a{5} = 99;
y = x in a;
put y=;
run;
Log 6.2 Results from Using the IN Operator to Search an Array of Numeric Values (Partial
Output)
173 data _null_;
174 array a{10} (2*1:5);
175 x=99;
176 y = x in a;
177 put y=;
178 a{5} = 99;
179 y = x in a;
180 put y=;
181 run;
y=0
y=1
Note: PROC SQL does not support this syntax.
Character Comparisons
You can perform comparisons on character operands, but the comparison always yields a
numeric result (1 or 0). Character operands are compared character by character from
left to right. Character order depends on the collating sequence, usually ASCII or
EBCDIC, used by your computer.
For example, in the EBCDIC and ASCII collating sequences, G is greater than A.
Therefore, this expression is true:
'Gray'>'Adams'
Two-character values of unequal length are compared as if blanks were attached to the
end of the shorter value before the comparison is made. A blank, or missing character
SAS Operators in Expressions 103
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.247.68