A quick tour of SAS operators

I want to understand the logic behind the answer to the following question:
The following DATA step is submitted:

data one;
 x=3;
 y=2;
 z=x**y;
 run;
What should be the value of the variable z in the output data set? Will you please explain what the ** symbol means, and how we solve the question?
-Too Many Asterisks
It is not intuitive that the ** symbol means exponentiation, as in X to the Yth power.  There are several arithmetic, comparison, and logical SAS operators (symbols), as well as miscellaneous operators for use in SAS functions and for grouping.  Let’s take a quick tour.

Arithmetic Operators

SymbolDefinitionExampleResult
**exponentiationa**3raise A to the third power
*multiplication12*ymultiply 2 by the value of Y
/divisionvar/5divide the value of VAR by 5
+additionnum+3add 3 to the value of NUM
-subtractionsale-discountsubtract 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.
Note:  If a missing value is an operand for an arithmetic operator, the result is a missing value.  You can use parentheses ( ) to control the order in which the expression is evaluated.

Comparison Operators

SymbolMnemonic EquivalentDefinitionExample
=EQequal toa=3
^=NEnot equal to1ane 3
¬=NEnot equal to
~=NEnot equal to
>GTgreater thannum>5
<LTless thannum<8
>=GEgreater than or equal to2sales>=300
<=LEless than or equal to3sales<=100
INequal to one of a listnumin (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.

Note:  You can add a colon (:) modifier to any of the operators to compare only a specified prefix of a character string (name =: ‘St’ would give you all the values of the name variable that start with a capital S followed by a lower case t).
In addition to the above comparison operators, there is the MIN (><) operator which returns the lesser of the two values. The MAX (<>) operator returns the greater of the two values. For example, if A is less than B, then A><B returns the value of A (the minimum or lesser value).

 Logical Operators

SymbolMnemonic EquivalentExample
&AND(a>b & c>d)
|OR1(a>b or c>d)
!OR
¦OR
¬NOT2not(a>b)
NOT
~NOT
The symbol that you use for OR depend on your operating environment.
The symbol that you use for NOT depends on your operating environment.
Note:  Logical operators are also called Boolean operators.  For an AND expression to be true, both sides of the expression must be true (a must be greater than b as well as c must be greater than d, above).  For an OR expression to be true, only one side of the expression must be true (a must be greater than b or c must be greater than d, above).  The NOT logical operator is my favorite.  (Just listen to the name:  The not logicaloperator!  It’s what I tell my husband I’m being when I’m not following his rules of logic.)  Anyway, the NOT logical operator reverses the logic of the AND or OR expression (a cannot be greater than b, above).

Other Useful Symbols

The other useful symbol is the concatenation operator, which is represented by two vertical bars (||, Windows), two broken vertical bars (UNIX), or two exclamation points (!!).  Do remember, however, that the concatenation operator does NOT trim leading or trailing blanks from character values, which can lead to some bizarre results!
I hope this brief tour of some of the most used symbols in SAS code has been helpful.  I’m off now to use my NOT logical operator to good advantage. By the way, the answer to the question at the beginning is z is equal to 3 to the 2nd power or 9 (z=x**y;).

(courtesy: sasblogs)

ss logo-2small
SIGNETSOFT is the best Training center in Bangalore for SAS, SAS clinical, SAS projects, SAS Certification, Advanced Excel, VBA, Java, Clinical Data Management.
A practical approach! and real time expert faculty, good in placement record.
contact us: info@signetsoft.com
mob: 9844559330
Phone: 080-6547 2060

Comments

Popular posts from this blog