Dear Miss SAS Answers,
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
Dear Too Many,
I feel your pain. 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
Symbol | Definition | Example | Result |
** | exponentiation | a**3 | raise A to the third power |
* | multiplication1 | 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 |
1 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
Symbol | Mnemonic Equivalent | Definition | Example |
= | EQ | equal to | a=3 |
^= | NE | not equal to1 | ane 3 |
¬= | NE | not equal to | |
~= | NE | not equal to | |
> | GT | greater than | num>5 |
< | LT | less than | num<8 |
>= | GE | greater than or equal to2 | sales>=300 |
<= | LE | less than or equal to3 | sales<=100 |
IN | equal to one of a list | numin (3, 4, 5) | |
1 The symbol that you use for NE depends on your personal computer. | |||
2 The symbol => is also accepted for compatibility with previous releases of SAS. It is not supported in WHERE clauses or in PROC SQL. | |||
3 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
Symbol | Mnemonic Equivalent | Example |
& | AND | (a>b & c>d) |
| | OR1 | (a>b or c>d) |
! | OR | |
¦ | OR | |
¬ | NOT2 | not(a>b) |
∘ | NOT | |
~ | NOT | |
1 The symbol that you use for OR depends on your operating environment. | ||
2 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 logical operator! 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;).
-Miss SAS Answers
If you have a question for Miss SAS Answers leave a comment below!
10 Comments
I long ago grew to favor the use of EQ, NE, AND, OR, etc.
Apart from a basic weakness for clarity over concision, what convinced me was a remarkable experience back in the days of computer printers that were more dominantly mechanical. The letters and symbols to be struck were pieces of metal linked side-by-side on what was called a print chain. The print chain was mounted horizontally between two rotating vertical axes. The paper passed behind the print chain from top to bottom. There was a hammer or hammers mounted inside the loop of the chain. When it was time to strike the next letter to be printed on the line, as soon as the rotating chain presented it in the right location, a hammer would strike it. I'm not sure whether the various letters and symbols were present in the chain only once. (One might think that, like in the word game Scrabble, the more frequently used letters should be present more often.) At any rate, for a symbol which is unlikely to be used, or for which there are substitutes, the speed of printing was maximized by omitting it from the print chain. Now that I have held you spellbound by this tidbit from the history of computer printing, let me get to the point of the story.
In one of my nightly production programs, I had coded a statement something like
IF X ¬= '7' THEN . . . ;
I was unavailable, and the program had a malfunction. When my backup studied the printed SAS log, he was puzzled by the behavior of the program given the presence of the statement
IF X = '7' THEN . . . ;
I enjoyed Miss SAS Answers explanation, but I would dare to suggest that symbols/operators like <> and >< which need an anti-intuitive explanation are mainly tools for either the obsessively typing averse or programmers who are not attentive to the understanding needs of someone else who might need to read their code.
Pingback: Top 10 SAS Training Post blogs of 2013 | The SAS Training Post
Thanks for finally writing about > A quick tour of SAS operators - The SAS Training Post < Liked it!
the New&Improved v9.4 page is:
http://support.sas.com/documentation/cdl/en/lrcon/64801/HTML/default/viewer.htm#p00iah2thp63bmn1lt20esag14lh.htm
Thanks for the update Ronald!
Pingback: URL
Pingback: Syntactic sugar – 6 SAS essentials - The SAS Training Post
Thanks Linda for this concise explanation of SAS operators.
It's also important to know the order in which expressions are evaluated. This is sometimes called the "precedence" of an operator. In school we learned the precendence of arithmetic operators through the mnemonic "Please excuse my dear Aunt Sally," which reminds us that the order of evaluation is Parentheses, Exponents, Multiplication/Division, and Addition/Subtraction. Because SAS has other operators (comparison and logical), see the following documentation for the full list:
http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000780367.htm
In the SAS/IML language, there are even more operators. I wrote a blog post on how to remember some of the operators in that language: http://blogs.sas.com/content/iml/2011/08/08/please-eat-my-dear-aunt-sallys-italian-lasagna/
Thank you for this explanation,, it has cleared my concerns about the question..