Earlier lessons covered character functions (L101, L102), type conversion functions INPUT and PUT (L103), and cross-variable aggregation functions N/NMISS/SUM/MEAN/MIN/MAX (L201).
This lesson covers the core numeric math functions: ROUND, INT, FLOOR, CEIL, MOD, and ABS.
These functions are used constantly for rounding results, extracting integer parts, finding remainders, and working with absolute differences — all common in data step calculations and reporting.
ROUND - Rounding to a Specified Unit
ROUND(value, unit) rounds the value to the nearest multiple of unit.
The unit argument controls the precision: ROUND(x, 1) rounds to the nearest whole number, ROUND(x, 0.1) rounds to one decimal place, ROUND(x, 10) rounds to the nearest ten.
ROUND follows standard rounding rules: values exactly halfway between two multiples round away from zero (e.g., ROUND(2.5, 1) = 3).
ROUND is the correct function for display rounding in clinical analysis — always specify the unit explicitly rather than relying on format truncation alone.
SAS Log
Check WORK.ROUNDED carefully — floating-point arithmetic can occasionally produce unexpected results at the boundary (e.g., 12.345 may round to 12.34 rather than 12.35 due to binary representation).
round_int and ROUND(x, 1) produce the same result as standard rounding to the nearest integer.
round_10 rounds to the nearest ten: 99.9 rounds to 100, 12.3 rounds to 10.
For negative values: -7.65 rounded to 0.1 gives -7.7 (rounding away from zero).
Dataset View
INT - Integer Truncation Toward Zero
INT(value) returns the integer portion of a number by truncating toward zero — it does not round, it simply drops the fractional part.
For positive numbers, INT(x) is the same as FLOOR(x). For negative numbers, the difference matters: INT(-2.7) = -2, FLOOR(-2.7) = -3.
INT is commonly used when you need to count how many complete units fit into a value — for example, converting a total number of days into complete weeks: INT(days / 7).
SAS Log
Confirm the difference between INT and FLOOR for negative values: INT(-7.9) = -7, FLOOR(-7.9) = -8.
CEIL (ceiling) is the opposite of FLOOR: it rounds up to the nearest integer away from zero in the positive direction.
FLOOR and CEIL are equivalent to rounding down and up respectively for positive numbers. For negative numbers, both shift away from zero in opposite directions.
Inspect WORK.INT_RESULT to confirm all three functions behave as described for both positive and negative inputs.
Dataset View
FLOOR and CEIL in Practice
FLOOR(value) returns the largest integer less than or equal to value — effectively always rounding down.
CEIL(value) returns the smallest integer greater than or equal to value — effectively always rounding up.
FLOOR is used when you want the complete lower unit (e.g., completed months of treatment). CEIL is used when you want to ensure at least one unit (e.g., minimum number of pages needed).
SAS Log
Subject with 27 days: FLOOR(27/7) = FLOOR(3.857) = 3 complete weeks; CEIL(27/7) = CEIL(3.857) = 4 weeks needed to cover the period.
Subject with 28 days: exactly 4 weeks — FLOOR and CEIL both return 4.
FLOOR and CEIL return the same value whenever the input is already an exact integer multiple of the divisor.
Inspect WORK.WEEKS to verify the complete_weeks and weeks_needed columns for each subject.
Dataset View
MOD - Remainder After Division
MOD(dividend, divisor) returns the remainder after dividing dividend by divisor.
The result has the same sign as the dividend (not the divisor). MOD(10, 3) = 1, MOD(-10, 3) = -1.
MOD is used to test divisibility (MOD(n, 2) = 0 means n is even), to cycle through repeating sequences (e.g., alternating group assignment), and to extract time components from raw minute counts.
is_even = 1 when total_minutes is divisible by 2 (no remainder), 0 otherwise.
This INT + MOD pattern is the standard way to decompose a total into units and sub-units without using datetime functions.
Dataset View
ABS - Absolute Value
ABS(value) returns the absolute (non-negative) value of a number. Negative values become positive; positive values and zero are unchanged.
ABS is used when the magnitude of a difference matters but not its direction — for example, when computing the absolute difference between two measurements to find the larger of two deviations.
ABS is also useful in tolerance comparisons: if abs(a - b) is less than a threshold, a and b are considered equal.
SAS Log
raw_diff can be positive or negative depending on direction; abs_diff is always non-negative.
within_5pct flags rows where the absolute difference is within 5% of the expected value: abs(actual - expected) / expected <= 0.05.
Subject 002 has actual = 103, raw_diff = +3, abs_diff = 3, within_5pct = 1 (3/100 = 0.03, which is within 5%).
Subject 005 has actual = 107, abs_diff = 7, within_5pct = 0 (7/100 = 0.07, which exceeds 5%).
Inspect WORK.ABS_RESULT to confirm all flags are correct.
Dataset View
Key Points
ROUND(value, unit) rounds to the nearest multiple of unit — always specify the unit explicitly (e.g., 0.1 for one decimal place).
INT truncates toward zero, dropping the fractional part without rounding — use it when counting complete units.
FLOOR rounds down to the largest integer at or below the value; CEIL rounds up to the smallest integer at or above the value. They differ for negative numbers.
MOD(dividend, divisor) returns the remainder — use it to test divisibility, cycle through patterns, or decompose totals into units and sub-units.
ABS returns the magnitude of a value regardless of sign — use it when direction does not matter but size does, such as in tolerance checks and deviation calculations.