Working with Numeric Variables |
Using Arithmetic Operators in Assignment Statements |
One way to perform calculations on numeric variables is to write an assignment statement using arithmetic operators. Arithmetic operators indicate addition, subtraction, multiplication, division, and exponentiation (raising to a power). For more information on arithmetic expressions, see the discussion in SAS Language Reference: Concepts. The following table shows operators that you can use in arithmetic expressions.
Operation | Symbol | Example |
---|---|---|
addition | + | x = y + z; |
subtraction | - | x = y - z; |
multiplication | * | x = y * z |
division | / | x = y / z |
exponentiation | ** | x = y ** z |
The following examples show some typical calculations using the Tradewinds Travel sample data.
Action | SAS Statement |
Add the airfare and land cost to produce the total cost. |
TotalCost = AirCost + Landcost; |
Calculate the peak season airfares by increasing the basic fare by 10% and adding an $8 departure tax. |
PeakAir = (AirCost * 1.10) + 8; |
Show the cost per night of each land package. |
NightCost = LandCost / Nights; |
In each case, the variable on the left side of the equal sign receives the calculated value from the numeric expression on the right side of the equal sign. Including these statements in the following DATA step produces data set NEWTOUR:
options pagesize=60 linesize=80 pageno=1 nodate; data newtour; set mylib.populartours; TotalCost = AirCost + LandCost; PeakAir = (AirCost * 1.10) + 8; NightCost = LandCost / Nights; run; proc print data=newtour; var Country Nights AirCost LandCost TotalCost PeakAir NightCost; title 'Costs for Tours'; run;
The VAR statement in the PROC PRINT step causes only the variables listed in the statement to be displayed in the output.
Creating New Variables by Using Arithmetic Expressions
Costs for Tours 1 Air Land Total Peak Night Obs Country Nights Cost Cost Cost Air Cost 1 Japan 8 982 1020 2002 1088.2 127.500 2 Greece 12 . 748 . . 62.333 3 New Zealand 16 1368 1539 2907 1512.8 96.188 4 Ireland 7 787 628 1415 873.7 89.714 5 Venezuela 9 426 505 931 476.6 56.111 6 Italy 8 852 598 1450 945.2 74.750 7 Russia 14 1106 1024 2130 1224.6 73.143 8 Switzerland 9 816 834 1650 905.6 92.667 9 Australia 12 1299 1169 2468 1436.9 97.417 10 Brazil 8 682 610 1292 758.2 76.250
Understanding Numeric Expressions and Assignment Statements |
Numeric expressions in SAS share some features with mathematical expressions:
When an expression contains more than one operator, the operations have the same order of precedence as in a mathematical expression: exponentiation is done first, then multiplication and division, and finally addition and subtraction.
When operators of equal precedence appear, the operations are performed from left to right (except exponentiation, which is performed right to left).
Parentheses are used to group parts of an expression; as in mathematical expressions, operations in parentheses are performed first.
Note: The equal sign in an assignment statement does not perform the same function as the equal sign in a mathematical equation. The sequence variable= in an assignment statement defines the statement, and the variable must appear on the left side of the equal sign. You cannot switch the positions of the result variable and the expression as you can in a mathematical equation.
Understanding How SAS Handles Missing Values |
What if an observation lacks a value for a particular numeric variable? For example, in the data set MYLIB.POPULARTOURS, as shown in Creating New Variables by Using Arithmetic Expressions, the observation for Greece has no value for the variable AirCost. To maintain the rectangular structure of a SAS data set, SAS assigns a missing value to the variable in that observation. A missing value indicates that no information is present for the variable in that observation.
The following rules describe missing values in several situations:
In data lines, a missing numeric value is represented by a period, for example,
Greece 8 12 . 748 Express
By default, SAS interprets a single period in a numeric field as a missing value. (If the INPUT statement reads the value from particular columns, as in column input, a field that contains only blanks also produces a missing value.)
In an expression, a missing numeric value is represented by a period, for example,
if AirCost= . then Status = 'Need air cost';
In a comparison and in sorting, a missing numeric value is a lower value than any other numeric value.
In procedure output, SAS by default represents a missing numeric value with a period.
Some procedures eliminate missing values from their analyses; others do not. Documentation for individual procedures describes how each procedure handles missing values.
When you use a missing value in an arithmetic expression, SAS sets the result of the expression to missing. If you use that result in another expression, the next result is also missing. In SAS, this method of treating missing values is called propagation of missing values. For example, Creating New Variables by Using Arithmetic Expressions shows that in the data set NEWTOUR, the values for TOTALCOST and PEAKAIR are also missing in the observation for Greece.
Note: SAS enables you to distinguish between various kinds of numeric missing values. See "Missing Values" section of SAS Language Reference: Concepts. The SAS language contains 27 special missing values based on the letters A-Z and the underscore (_).
Calculating Numbers Using SAS Functions |
In the example data that lists costs of the different tours (Data Set MYLIB.POPULARTOURS), some of the tours have odd prices: $748 instead of $750, $1299 instead of $1300, and so on. Rounded numbers, created by rounding the tour prices to the nearest $10, would be easier to work with.
Programming a rounding calculation with only the arithmetic operators is a lengthy process. However, SAS contains around 280 built-in numeric expressions called functions. You can use them in expressions just as you do the arithmetic operators. For example, the following assignment statement rounds the value of AirCost to the nearest $50:
RoundAir = round(AirCost,50);
The following statement calculates the total cost of each tour, rounded to the nearest $100:
TotalCostR = round(AirCost + LandCost,100);
As another example, the travel agent can calculate a total cost for the tours based on all nonmissing costs. Therefore, when the airfare is missing (as it is for Greece) the total cost represents the land cost, not a missing value. (Of course, you must decide whether skipping missing values in a particular calculation is a good idea.) The SUM function calculates the sum of its arguments, ignoring missing values. This example illustrates the SUM function:
SumCost = sum(AirCost,LandCost);
It is possible for you to combine functions. The ROUND function rounds the quantity given in the first argument to the nearest unit given in the second argument. The SUM function adds any number of arguments, ignoring missing values. The calculation in the following assignment statement rounds the sum of all nonmissing airfares and land costs to the nearest $100 and assigns the value to RoundSum:
RoundSum = round(sum(AirCost,LandCost),100);
Using the ROUND and SUM functions in the following DATA step creates the data set MORETOUR:
options pagesize=60 linesize=80 pageno=1 nodate; data moretour; set mylib.populartours; RoundAir = round(AirCost,50); TotalCostR = round(AirCost + LandCost,100); CostSum = sum(AirCost,LandCost); RoundSum = round(sum(AirCost,LandCost),100); run; proc print data=moretour; var Country AirCost LandCost RoundAir TotalCostR CostSum RoundSum; title 'Rounding and Summing Values'; run;
The following output displays the results:
Creating New Variables with ROUND and SUM Functions
Rounding and Summing Values 1 Air Land Round Total Cost Round Obs Country Cost Cost Air CostR Sum Sum 1 Japan 982 1020 1000 2000 2002 2000 2 Greece . 748 . . 748 700 3 New Zealand 1368 1539 1350 2900 2907 2900 4 Ireland 787 628 800 1400 1415 1400 5 Venezuela 426 505 450 900 931 900 6 Italy 852 598 850 1500 1450 1500 7 Russia 1106 1024 1100 2100 2130 2100 8 Switzerland 816 834 800 1700 1650 1700 9 Australia 1299 1169 1300 2500 2468 2500 10 Brazil 682 610 700 1300 1292 1300
Copyright © 2012 by SAS Institute Inc., Cary, NC, USA. All rights reserved.