4.2 - Arithmetic Calculations Using Arithmetic Operators

All we've done so far is add variables together. Of course, we could also subtract, multiply, divide, or exponentiate variables. We just have to make sure that we use the symbols that SAS recognizes. They are:

Operation

Symbol

Assignment Statement

Action Taken

addition

+

a = b + c;

add b and c

subtraction

-

a = b - c;

subtract c from b

multiplication

*

a = b * c;

multiply b and c

division

/

a = b / c;

divide b by c

exponentiation

**

a = b ** c;

raise b to the power of c

negative prefix

-

a = -b;

take the negative of b

As is the case in other programming languages, you can perform more than one operation in an assignment statement. The operations are performed as they are for any mathematical expression, namely:

  • exponentiation is performed first, then multiplication and division, and finally addition and subtraction
  • if multiple instances of addition, multiple instances of subtraction, or addition and subtraction appear together in the same expression, the operations are performed from left to right
  • if multiple instances of multiplication, multiple instances of division, or multiplication and division appear together in the same expression, the operations are performed from left to right
  • if multiple instances of exponentiation occur in the same expression, the operations are performed right to left
  • operations in parentheses are performed first

It's that last bullet that I think is the most helpful to know. If you use parentheses to specifically tell SAS what you want to be calculated first, then you needn't worry as much about the other rules. Let's take a look at two examples.

Example 4.4 Section

The following example contains a calculation that illustrates the standard order of operations. Suppose a statistics instructor calculates the final grade by weighting the average exam score by 0.6, the project score by 0.2, and the final exam by 0.2. The following SAS program illustrates how the instructor (incorrectly) calculates the students' final grades:

DATA grades;
	input name $ 1-15 e1 e2 e3 e4 p1 f1;
	final = 0.6*e1+e2+e3+e4/4 + 0.2*p1 + 0.2*f1;
	DATALINES;
Alexander Smith  78 82 86 69  97 80
John Simon       88 72 86  . 100 85
Patricia Jones   98 92 92 99  99 93
Jack Benedict    54 63 71 49  82 69
Rene Porter     100 62 88 74  98 92
;
RUN;

PROC PRINT data = grades;
	var name e1 e2 e3 e4 p1 f1 final;
RUN;

Well, okay, so the instructor should stick to statistics and not mathematics. As you can see in the assignment statement, the instructor is attempting to tell SAS to average the four exam scores by adding them up and dividing by 4, and then multiplying the result by 0.6. Let's see what SAS does instead. Launch and run  the SAS program, and review the output to see if you can figure out what SAS did, say, for the first student Alexander Smith. If you're still not sure, review the rules for the order of the operations again. The rules tell us that SAS first:

  • takes Alexander's first exam score of 78 and multiples it by 0.6 to get 46.8
  • takes Alexander's fourth exam score of 69 and divides it by 4 to get 17.25
  • takes Alexander's project score of 97 and multiplies it by 0.2 to get 19.4
  • takes Alexander's final exam score of 80 and multiplies it by 0.2 to get 16.0

Then, SAS performs all of the addition:

46.8 + 82 + 86 + 17.25 + 19.4 + 16.0

to get his final score of 267.45. Now, maybe that's the final score that Alexander wants, but it is still fundamentally wrong. Let's see if we can help set the statistics instructor straight by taking advantage of that last rule that says operations in parentheses are performed first.

Example 4.5 Section

The following example contains a calculation that illustrates the standard order of operations. Suppose a statistics instructor calculates the final grade by weighting the average exam score by 0.6, the project score by 0.2, and the final exam by 0.2. The following SAS program illustrates how the instructor (correctly) calculates the students' final grades:

DATA grades;
	input name $ 1-15 e1 e2 e3 e4 p1 f1;
	final = 0.6*((e1+e2+e3+e4)/4) + 0.2*p1 + 0.2*f1;
	DATALINES;
Alexander Smith  78 82 86 69  97 80
John Simon       88 72 86  . 100 85
Patricia Jones   98 92 92 99  99 93
Jack Benedict    54 63 71 49  82 69
Rene Porter     100 62 88 74  98 92
;
RUN;

PROC PRINT data = grades;
	var name e1 e2 e3 e4 p1 f1 final;
RUN;

Let's dissect the calculation of Alexander's final score again. The assignment statement for final tells SAS:

  • to first add Alexander's four exam scores (78, 82, 86, 69) to get 315
  • and then divide that total 315 by 4 to get an average exam score of 78.75
  • and then multiply the average exam score of 78.75 by 0.6 to get 47.25
  • and then take Alexander's project score of 97 and multiply it by 0.2 to get 19.4
  • and then take Alexander's final exam score of 80 and multiply it by 0.2 to get 16.0

Then, SAS performs the addition of the last three items:

47.25 + 19.4 + 16.0

to get his final score of 82.65. There, that sounds much better. Sorry, Alexander.

Launch and run  the SAS program to see how we did. Review the output from the print procedure to convince yourself that the final grades have been calculated as the instructor wishes. By the way, note again that SAS assigns a missing value to the final grade for John Simon.

In this last example, we calculated the students' average exam scores by adding up their four exam grades and dividing them by 4. We could have instead taken advantage of one of the many numeric functions that are available in SAS, namely that of the MEAN function.