SAS Operators Guide – Everything You Need to Know
In this SAS tutorial, we will study SAS Operators, types of SAS Operators; Arithmetic Operators, Logical Operators, and Comparison Operators. We will also see Symbol, expression, examples of operators in SAS Programming Language. At last, we will see some sub-type of these SAS Operators.
Introduction to SAS Operators
Operators in SAS are symbols that are used to perform calculations such as (mathematical, arithmetic, logical, comparison) between two expressions or two variables.
Must learn – How to create variables in SAS
Types of Operators in SAS
All operators in SAS are in-built. Below are the three most important types of operators present in SAS –
- Arithmetic Operators in SAS
- Logical Operators in SAS
- Comparison Operators in SAS
1. SAS Arithmetic Operators
These SAS Operators are used to show that an arithmetic calculation is being performed.
|**||exponentiation||a**3||raise A to the third power|
|*||multiplication||2*y||multiply 2 by the value of Y|
|/||division||var/4||divide the value of VAR by 4|
|+||addition||num+4||add 4 to the value of NUM|
|–||subtraction||sale-discount||subtract the value of DISCOUNT from the value of SALE|
Understand the concept of SAS Data Set
2. SAS Logical Operators
Logical operators are also called Boolean operators. These SAS Operators are usually used in expressions to construct logical sequences of comparisons. The logical operators are shown in the following table:
|&||AND||(a>b & c>d)|
|(a>b or c>d)|
An example of a SAS statement using a logical operator is shown below:
if age < 25 and sex = “F” then youngfem = 1;
Parentheses can be used in SAS expressions to group parts of the expression and control the order in which operations are processed. Expressions within parentheses are evaluated before anything outside of parentheses is checked. Be sure that each left parenthesis is followed by a matching right parenthesis.
if (age < 25) and (sex = “F”) then youngfem = 1;
Below, we use AND to output observations representing Australian sales associates that make less than $30,000 a year. Both the symbol and mnemonic are used and they give the same result.
PROC PRINT DATA=idre.sales; WHERE Country='AU' AND Salary<30000; RUN;
PROC PRINT data=idre.sales; WHERE Country='AU' & Salary<30000; RUN;
With comparison operators, you can also combine AND Operator, OR operator & NOT operator with the IN operator. In the example below, the variable job_title includes seven different job types. We want to obtain frequencies of all of them except for two, Sales Manager and Sales Rep IV. So, we use the NOT combined with IN since we have more than one value that we are trying to exclude.
PROC FREQ DATA=idre.sales; TABLES Job_Title; WHERE Job_Title NOT IN ('Sales Manager','Sales Rep. IV'); RUN;
3. SAS Comparison Operators
Comparison operators set up a comparison, operation, or calculation of two variables, constants, or expressions. If the comparison is true, the result is 1. If the comparison is false, the result is 0.
These operators can be expressed as symbols or with their mnemonic equivalents, which are shown in the following table:
|^=||not equal to||a ne 2|
|>=||greater than or equal to||sales>=200|
|<=||less than or equal to||sales<=200|
One of the simplest ways to use a comparison operator is in a WHERE statement. In the “sales” data file, we have information on sales associates from Australia (AU) and the United States (US). If we only wanted to output records for Australian sales associates, we could use the = or eq operator. Since the variable country contains character information is not numeric, we need to put single quotes around ‘AU’.
PROC PRINT DATA=idre.sales; WHERE Country='AU'; RUN;
The IN operator in SAS can be used if you are trying to specify a list or range of values, as demonstrated below.
PROC PRINT DATA=idre.sales; WHERE Country IN ('AU', 'US'); RUN;
We can also specify SAS to output only certain ranges of values for numeric variables. In the first example below, we ask SAS to output salary values that are less than (<) $30,000. In the second example, we output salary values greater than or equal to $30,000.
PROC PRINT DATA=idre.sales; WHERE Salary<30000; RUN;
PROC PRINT DATA=idre.sales; WHERE Salary ge 30000; RUN;
Have you checked? – SAS Functions
Order of Precedence of Operators
The order of precedence of the arithmetic operators and Boolean operators in SAS Programming is shown below. Items in higher row are evaluated before those in a lower row, and items in the same row are evaluated generally from left to right.
* and /
+ and –
< < + = ~= >= > ~< ~>
& (AND) | (OR)
This was all on SAS Operators. Hope you liked our explanation.
This section was all about what are SAS operators, different types of operators in SAS and some examples of SAS operators showing them. Don’t they make evaluating expressions easier? We will learn about other important functions in SAS in the upcoming articles.
Did you face difficulty in any of the operators? Enter in the comments section. We will definitely provide you with a quick solution.