SAS: Numeric Functions (with Examples)

Deepanshu Bhalla Add Comment

In this post we will cover various numeric functions in SAS, along with examples.

List of most frequently used numeric functions in SAS

See the syntax of SAS numeric functions including their description.

Functions Syntax Description
MIN var1 = min(x, y, z); minimum value
MAX var1 = max(x, y, z); maximum value
SUM var1 = sum(x, y, z); sum
INT var1 = int(x); integer portion of a numeric value
ABS var1 = abs(x); absolute value
SQRT var1 = sqrt(x); square root
MEAN var1 = mean(x, y, z); mean
MEDIAN var1 = median(x, y, z); median or middle value
ROUND var1 = round(x, 1); round the argument to the specified unit
CEIL var1 = ceil(x); Smallest integer that is greater than or equal to the argument
FLOOR var1 = floor(x); largest integer that is less than or equal to the argument
LOG var1 = log(x); log of base e
LOG10 var1 = log10(x); log of base 10
EXP var1 = exp(x); value of the exponential function
LAG var1 = lag(x); value in the previous observation
DIF var1 = dif(x); difference between the values in the current and previous observations
N var1 = n(x); number of non-missing values
NMISS var1 = nmiss(x); number of missing values
coalesce var1 = coalesce(of x1-x3); It returns the first non-missing value.
Create SAS Dataset

Let's create a sample SAS dataset for the examples in this tutorial.

data have;
input sale1 sale2 sale3;
cards;
12 24 17
25 . 67
20 39 44
34 69 82
;
run;
Difference between SUM Function and + Operator

The SUM function handles missing values when calculating the sum, whereas the + operator does not return SUM if missing value(s) exist in any of the variable.

data want;
set have;
newsale  = sale1 + sale2 + sale3;
newsale1 = sum(sale1, sale2, sale3);
run;
SAS: Difference between SUM Function and + Operator

The above code creates a new SAS dataset named "want" using the "have" dataset as input. Two new variables, "newsale" and "newsale1," are calculated using the sum of "sale1," "sale2," and "sale3." Finally, the "run" statement ends the data step. You may have noticed the second value of "newsale" is blank whereas it is not blank for "newsale1".

How to use Numeric Functions in SAS
data want;
set have;
avg = mean(sale1, sale2, sale3);
middle = median(sale1, sale2, sale3);
count = n(sale1, sale2, sale3);
nonmissing = nmiss(sale1, sale2, sale3);
run;

You can also use the of keyword with a variable range (sale1-sale3) to specify that the functions should be applied to all variables within that range. The code below returns the similar output as output generated from the code shown in the previous section.

data want;
  set have;
  avg = mean(of sale1-sale3);
  middle = median(of sale1-sale3);
  count = n(of sale1-sale3);
  nonmissing = nmiss(of sale1-sale3);
run;
SAS Numeric Functions
Explanation
  • avg variable is assigned the mean value of sale1, sale2, and sale3 using the mean function.
  • middle variable is assigned the median value of sale1, sale2, and sale3 using the median function.
  • count variable is assigned the count of non-missing values of sale1, sale2, and sale3 using the n function.
  • nonmissing variable is assigned the count of missing values of sale1, sale2, and sale3 using the nmiss function.
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "SAS: Numeric Functions (with Examples)"
Next → ← Prev