5.2 Numeric & Mathematical Functions
Key Takeaways
- The SUM function calculates the sum of non-missing arguments, whereas the addition operator (+) returns a missing value if any operand is missing.
- ROUND(argument, round-off-unit) rounds values to the nearest multiple of the specified unit, such as 0.01 for currency cents.
- INT truncates decimal values toward zero, while FLOOR returns the greatest integer less than or equal to the argument and CEIL returns the smallest integer greater than or equal to the argument.
- SAS statistical functions (MEAN, MIN, MAX, SUM, N, NMISS) automatically ignore missing values in their argument lists.
- Variable list shortcuts in SAS functions require the OF keyword (e.g., SUM(OF x1-x10)) to prevent SAS from interpreting hyphens as subtraction operators.
5.2 Numeric & Mathematical Functions
Numeric processing is at the heart of SAS programming. Whether performing financial calculations, data transformations, or descriptive statistical analysis, SAS programmers rely on built-in numeric functions. A critical requirement for the SAS Base Certification exam is knowing how SAS numeric functions handle missing data, rounding precision, integer truncation, mathematical transformations, and variable list arguments.
1. Rounding and Truncation Functions
SAS provides several functions to round or truncate floating-point numbers into integers or specified decimal increments.
The ROUND Function
The ROUND function rounds a numeric value to the nearest multiple of a specified round-off unit.
Syntax: ROUND(argument, <round-off-unit>)
- If
round-off-unitis omitted, SAS rounds to the nearest integer (default unit =1). - If the argument is halfway between two multiples, SAS rounds up to the larger value.
data work.round_demo;
val1 = round(12.3456, 0.01); /* 12.35 (rounds to nearest hundredth) */
val2 = round(12.3456, 0.1); /* 12.3 (rounds to nearest tenth) */
val3 = round(12.3456); /* 12 (rounds to nearest integer) */
val4 = round(148.50, 10); /* 150 (rounds to nearest ten) */
run;
Comparing INT, FLOOR, and CEIL
Understanding the differences between integer truncation functions is frequently tested on the SAS certification exam, especially when operating on negative numbers.
| Function | Mathematical Definition | Positive Example (3.7) | Negative Example (-3.7) |
|---|---|---|---|
INT(x) | Truncates decimal portion (rounds toward zero) | 3 | -3 |
FLOOR(x) | Greatest integer less than or equal to x | 3 | -4 |
CEIL(x) | Smallest integer greater than or equal to x | 4 | -3 |
data work.trunc_demo;
pos = 5.8;
neg = -5.8;
int_pos = int(pos); /* 5 */
floor_pos = floor(pos); /* 5 */
ceil_pos = ceil(pos); /* 6 */
int_neg = int(neg); /* -5 (truncates decimal) */
floor_neg = floor(neg); /* -6 (rounds down away from zero) */
ceil_neg = ceil(neg); /* -5 (rounds up toward zero) */
run;
2. Advanced Mathematical Utility Functions (MOD, SQRT, ABS, LOG)
In addition to rounding, Base SAS includes mathematical functions for algebra, remainder arithmetic, and absolute value calculations.
The MOD Function (Remainder Division)
MOD(n, d) returns the remainder when n is divided by d.
data work.mod_demo;
rem1 = mod(10, 3); /* Returns 1 (10 / 3 = 3 remainder 1) */
rem2 = mod(14, 5); /* Returns 4 */
run;
/* Exam Application: splitting even and odd observation numbers.
Every data set written by OUTPUT must be named on the DATA statement. */
data work.even_rows work.odd_rows;
set work.transactions;
if mod(_n_, 2) = 0 then output work.even_rows;
else output work.odd_rows;
run;
Additional Mathematical Functions
ABS(x): Returns the absolute (positive) value ofx(e.g.,ABS(-15)=15).SQRT(x): Returns the square root ofx. Ifxis negative, SAS returns a missing value (.) and prints an invalid argument note to the log.LOG(x)/EXP(x): Returns the natural logarithm or exponential power ($e^x$) ofx.SIGN(x): Returns-1ifx < 0,0ifx = 0, and1ifx > 0.
Order-Statistic and Random Functions Named in the Exam Content Guide
The official A00-231 content guide explicitly names SMALLEST, LARGEST, and RAND alongside SUM, MEAN, ROUND, and INT, so expect at least one item drawn from them.
SMALLEST(k, value-1, ..., value-n): Returns the k-th smallest non-missing value from the argument list.SMALLEST(1, 40, ., 12, 75)returns12;SMALLEST(2, 40, ., 12, 75)returns40.LARGEST(k, value-1, ..., value-n): Returns the k-th largest non-missing value.LARGEST(1, 40, ., 12, 75)returns75;LARGEST(2, ...)returns40.RAND('distribution', <parameters>): Returns a random number from the named distribution.RAND('UNIFORM')returns a value in the interval (0, 1);RAND('NORMAL', 100, 15)returns a normal draw with mean 100 and standard deviation 15;RAND('INTEGER', 1, 6)simulates a die roll.
data work.order_stats;
a = 40; b = .; c = 12; d = 75;
low1 = smallest(1, a, b, c, d); /* 12 - missing values are skipped */
low2 = smallest(2, a, b, c, d); /* 40 */
high1 = largest(1, a, b, c, d); /* 75 */
spread = high1 - low1; /* 63 */
run;
/* RAND requires a seed for reproducible streams */
data work.simulated;
call streaminit(20260807); /* fixes the random stream */
do trial = 1 to 5;
u = rand('UNIFORM');
die = rand('INTEGER', 1, 6);
output;
end;
run;
Exam Tip:
SMALLEST(k, ...)andLARGEST(k, ...)take the rank first and the values afterwards, and both ignore missing values.RANDneedsCALL STREAMINIT(seed);before the first call if the results must be reproducible; without it SAS seeds from the system clock.
3. Descriptive Statistics Functions & Missing Values
SAS DATA step functions can calculate summary statistics across variables within a single observation (row-wise processing).
Common statistical functions include:
SUM(arg1, arg2, ...): Calculates the total of non-missing values.MEAN(arg1, arg2, ...): Calculates the arithmetic average of non-missing values.MIN(arg1, arg2, ...)/MAX(arg1, arg2, ...): Identifies the minimum or maximum non-missing value.N(arg1, arg2, ...): Counts the number of non-missing numeric values.NMISS(arg1, arg2, ...): Counts the number of missing numeric values.STD(arg1, arg2, ...)/VAR(arg1, arg2, ...): Computes the standard deviation or variance across row arguments.
Critical Difference: SUM Function vs. Addition Operator (+)
One of the most important concepts in SAS programming is the handling of missing values (.) when performing arithmetic calculations.
data work.missing_comparison;
x = 10;
y = .;
z = 30;
/* Using Addition Operator */
total_op = x + y + z; /* Result: . (Missing!) */
/* Using SUM Function */
total_fn = sum(x, y, z); /* Result: 40 (Ignores missing value) */
run;
Exam Rule: If any argument in an expression using the
+operator is missing, SAS evaluates the entire expression as a missing value (.). In contrast, theSUMfunction ignores missing values and calculates the sum of all remaining valid numeric arguments. If all arguments passed toSUMare missing,SUMreturns a missing value.
4. Using Variable Lists with the OF Keyword
When passing a list or range of variables to a SAS function, you must include the keyword OF before the variable list.
Variable List Syntax Types:
- Numbered Range:
sum(of qtr1-qtr4)(expands toqtr1, qtr2, qtr3, qtr4) - Name Range:
mean(of height--weight)(includes all variables betweenheightandweightin dataset order) - Name Prefix:
max(of sales:)(includes all variables starting with the prefixsales) - All Numeric:
sum(of _numeric_)(includes all numeric variables in the dataset)
data work.variable_lists;
qtr1 = 100; qtr2 = 150; qtr3 = .; qtr4 = 200;
/* Correct syntax using OF keyword */
total_sales = sum(of qtr1-qtr4); /* Result: 450 */
avg_sales = mean(of qtr1-qtr4); /* Result: 150 (450 / 3 valid values) */
/* INCORRECT SYNTAX (Common Exam Trap!) */
/* diff = sum(qtr1 - qtr4); */
/* Without OF, SAS computes qtr1 minus qtr4 (100 - 200 = -100) and passes -100 to SUM! */
run;
Critical Exam Pitfall: Omitting the
OFkeyword insum(qtr1-qtr4)causes SAS to treat-as a subtraction operator rather than a range hyphen, yieldingsum(100 - 200)=-100instead of summing four variables!
Given the following DATA step: data work.calc; a = 15; b = .; c = 25; result1 = a + b + c; result2 = sum(a, b, c); run; What are the values of RESULT1 and RESULT2?
A SAS programmer submits the following program: data work.evaluate; val = -4.7; f_val = floor(val); c_val = ceil(val); i_val = int(val); run; What are the values of F_VAL, C_VAL, and I_VAL?
Consider the following DATA step code: data work.scores; test1 = 80; test2 = 90; test3 = 100; total1 = sum(of test1-test3); total2 = sum(test1 - test3); run; What are the values of TOTAL1 and TOTAL2?
What is the result of evaluating round(17.865, 0.01) in a SAS DATA step?