value, is smaller than any other printable character value. For example, because . is less
than
h, this expression is true:
'C. Jones'<'Charles Jones'
Since trailing blanks are ignored in a comparison, 'fox ' is equivalent to 'fox'.
However, because blanks at the beginning and in the middle of a character value are
significant to SAS,
' fox' is not equivalent to 'fox'.
You can compare only a specified prefix of a character expression by using a colon (:)
after the comparison operator. SAS truncates the longer value to the length of the shorter
value during the comparison. In the following example, the colon modifier after the
equal sign tells SAS to look at only the first character of values of the variable LastName
and to select the observations with names beginning with the letter S:
if lastname=:'S';
Because printable characters are greater than blanks, both of the following statements
select observations with values of LastName that are greater than or equal to the letter S:
if lastname>='S';
if lastname>=:'S';
Note: If you compare a zero-length character value with any other character value in
either an IN: comparison or an EQ: comparison, the two-character values are not
considered equal. The result always evaluates to 0, or false.
The operations that are discussed in this section show you how to compare entire
character strings and the beginnings of character strings. Several SAS character
functions enable you to search for and extract values from within character strings. See
SAS Functions and CALL Routines: Reference for complete descriptions of all SAS
functions.
The IN Operator in Character Comparisons
You can use the IN operator with character strings to determine whether a variable's
value is among a list of character values. The following statements produce the same
results:
if state in ('NY','NJ','PA') then region+1;
if state in ('NY' 'NJ' 'PA') then region+1;
if state='NY' or state='NJ' or state='PA' then region+1;
You can also use the IN operator to search an array of character 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.
data _null_;
array a{5} $ (5*'');
x='b1';
y = x in a;
put y=;
a{5} = 'b1';
y = x in a;
put y=;
run;
104 Chapter 6 Expressions
Log 6.3 Results from Using the IN Operator to Search an Array of Character Values (Partial
Output)
190 data _null_;
191 array a{5} $ (5*'');
192 x='b1';
193 y = x in a;
194 put y=;
195 a{5} = 'b1';
196 y = x in a;
197 put y=;
198 run;
y=0
y=1
Logical (Boolean) Operators and Expressions
Logical operators, also called Boolean operators, are usually used in expressions to link
sequences of comparisons. The logical operators are shown in the following table:
Table 6.5 Logical Operators
Symbol Mnemonic Equivalent Example
& AND
(a>b & c>d)
|
OR
*
(a>b or c>d)
! OR
¦ OR
¬
NOT
**
not(a>b)
NOT
~ NOT
*
The symbol that you use for OR depends on your operating environment.
**
The symbol that you use for NOT depends on your operating environment.
See “Order of Evaluation in Compound Expressions” on page 108 for the order in
which SAS evaluates these operators.
In addition, a numeric expression without any logical operators can serve as a Boolean
expression. For an example of Boolean numeric expressions, see “Boolean Numeric
Expressions” on page 107.
The AND Operator
If both of the quantities linked by AND are 1 (true), then the result of the AND operation
is 1. Otherwise, the result is 0. For example, in the following comparison,
a<b& c>0
SAS Operators in Expressions 105
the result is true (has a value of 1) only when both A<B and C>0 are 1 (true): that is,
when A is less than B and C is positive.
Two comparisons with a common variable linked by AND can be condensed with an
implied AND. For example, the following two subsetting IF statements produce the
same result:
if 16<=age and age<=65;
if 16<=age<=65;
The OR Operator
If either of the quantities linked by an OR is 1 (true), then the result of the OR operation
is 1 (true). Otherwise, the OR operation produces a 0. For example, consider the
following comparison:
a<b|c>0
The result is true (with a value of 1) when A<B is 1 (true) regardless of the value of C. It
is also true when the value of C>0 is 1 (true), regardless of the values of A and B.
Therefore, it is true when either or both of those relationships hold.
Be careful when using the OR operator with a series of comparisons (in an IF, SELECT,
or WHERE statement, for example). Remember that only one comparison in a series of
OR comparisons must be true to make a condition true, and any nonzero, nonmissing
constant is always evaluated as true. For more information about how SAS computes
Boolean expressions, see “Boolean Numeric Expressions” on page 107. Therefore, the
following subsetting IF statement is always true:
if x=1 or 2;
SAS first evaluates X=1, and the result can be either true or false. However, since the 2
is evaluated as nonzero and nonmissing (true), the entire expression is true. In this
statement, however, the condition is not necessarily true because either comparison can
evaluate as true or false:
if x=1 or x=2;
The NOT Operator
The prefix operator NOT is also a logical operator. The result of putting NOT in front of
a quantity whose value is 0 (false) is 1 (true). That is, the result of negating a false
statement is 1 (true). For example, if X=Y is 0 (false) then NOT(X=Y) is 1 (true). The
result of NOT in front of a quantity whose value is missing is also 1 (true). The result of
NOT in front of a quantity with a nonzero, nonmissing value is 0 (false). That is, the
result of negating a true statement is 0 (false).
For example, the following two expressions are equivalent:
not(name='SMITH')
name ne 'SMITH'
Furthermore, NOT(A&B) is equivalent to NOT A|NOT B, and NOT(A|B) is the same as
NOT A & NOT B. For example, the following two expressions are equivalent:
not(a=b & c>d)
a ne b | c le d
106 Chapter 6 Expressions
Boolean Numeric Expressions
In computing terms, a value of true is a 1 and a value of false is a 0. In SAS, any
numeric value other than 0 or missing is true, and a value of 0 or missing is false.
Therefore, a numeric variable or expression can stand alone in a condition. If its value is
a number other than 0 or missing, the condition is true. If its value is 0 or missing, the
condition is false.
0 | . = False
1 = True
For example, suppose that you want to fill in variable Remarks depending on whether
the value of Cost is present for a given observation. You can write the IF-THEN
statement as follows:
if cost then remarks='Ready to budget';
This statement is equivalent to:
if cost ne . and cost ne 0
then remarks='Ready to budget';
A numeric expression can be simply a numeric constant, as follows:
if 5 then do;
The numeric value that is returned by a function is also a valid numeric expression:
if index(address,'Avenue') then do;
The MIN and MAX Operators
The MIN and MAX operators are used to find the minimum or maximum value of two
quantities. Surround the operators with the two quantities whose minimum or maximum
value you want to know. The MIN (><) operator returns the lower of the two values. The
MAX (<>) operator returns the higher of the two values. For example, if A<B, then
A><B returns the value of A.
If missing values are part of the comparison, SAS uses the sorting order for missing
values that is described in “Order of Missing Values” on page 85. For example, the
maximum value that is returned by .A<>.Z is the value .Z.
Note: In a WHERE statement or clause, the <> operator is equivalent to NE.
The Concatenation Operator
The concatenation operator (||) concatenates character values. The results of a
concatenation operation are usually stored in a variable with an assignment statement, as
in level='grade '||'A'. The length of the resulting variable is the sum of the
lengths of each variable or constant in the concatenation operation, unless you use a
LENGTH or ATTRIB statement to specify a different length for the new variable.
The concatenation operator does not trim leading or trailing blanks. If variables are
padded with trailing blanks, check the lengths of the variables and use the TRIM
function to trim trailing blanks from values before concatenating them. See SAS
Functions and CALL Routines: Reference for descriptions and examples of additional
character functions.
SAS Operators in Expressions 107
For example, in this DATA step, the value that results from the concatenation contains
blanks because the length of the Color variable is eight:
data namegame;
length color name $8 game $12;
color='black';
name='jack';
game=color||name;
put game=;
run;
The value of Game is 'black jack'. To correct this problem, use the TRIM function
in the concatenation operation as follows:
game=trim(color)||name;
This statement produces a value of 'blackjack' for the variable Game. The following
additional examples demonstrate uses of the concatenation operator:
If A has the value 'fortune', B has the value 'five', and C has the value
'hundred', then the following statement produces the value
'fortunefivehundred' for the variable D:
d=a||b||c;
This example concatenates the value of a variable with a character constant.
newname='Mr.or Ms. ' ||oldname;
If the value of OldName is 'Jones', then NewName has the value 'Mr. or Ms.
Jones'
.
Because the concatenation operation does not trim blanks, the following expression
produces the value 'JOHN SMITH':
name='JOHN '||'SMITH';
This example uses the PUT function to convert a numeric value to a character value.
The TRIM function is used to trim blanks.
month='sep ';
year=99;
date=trim(month) || left(put(year,8.));
The value of DATE is the character value 'sep99'.
Order of Evaluation in Compound Expressions
Table 6.6 on page 109 shows the order of evaluation in compound expressions. The
table contains the following columns:
Priority
lists the priority of evaluation. In compound expressions, SAS evaluates the part of
the expression containing operators in Group I first, then each group in order.
Order of Evaluation
lists the rules governing which part of the expression SAS evaluates first.
Parentheses are often used in compound expressions to group operands; expressions
within parentheses are evaluated before those outside of them. The rules also list
how a compound expression that contains more than one operator from the same
group is evaluated.
108 Chapter 6 Expressions
..................Content has been hidden....................

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