Lesson 5: Common SAS Operators
In this lesson, we will explain the basic SAS operators (+-*/) and some simple calculation functions.
1. SAS Operators - Summation, Subtraction, Multiplication and Division
The dataset above contains the salaries for both Amy and John. We are going to add up the salary for each staff for the monthly pay cheque. Notice: Amy is missing the last week of work so that her salary in week 4 is missing.
There are two methods to add up a number:
Method 1: A = B + C (Not Recommended)
Method 2: A = sum (of B, C) (Recommended)
SALARY1 = WEEK1 + WEEK2 + WEEK3 + WEEK4;
SALARY2 = SUM (OF WEEK1, WEEK2, WEEK3, WEEK4);
As you can see, the two summation methods return the different results when there is a missing field (See Amy's Salary 1 and 2). Salary1, using summation method 1, returns a missing result since the Week-4 salary is missing. In contrast, Salary2 simply skips the missing salary and add up the 3 non-missing salaries from week1 to week3.
Method 2 is more commonly used as any missing value is being taken care of in the function.
Subtraction, Multiplication and Division
Subtraction, multiplication and division are quite straightforward:
The code below computes the BMI for each patients:
BMI = WEIGHT / (HEIGHT*HEIGHT);
DONE! You have learned the basic operators in SAS!