Search the site...

SASCRUNCH TRAINING
  • Home
  • Member's Area
  • How to Start
  • SAS Interface
  • Creating a Data Set
  • Practical SAS Training Course
  • SAS Certified Specialist Training Program
  • Proc SQL Course
  • Introduction to Time Series Analysis
  • SAS Project Training Course
  • Full Training / Membership
  • Sign up
  • About us
  • Contact us
  • Home
  • Member's Area
  • How to Start
  • SAS Interface
  • Creating a Data Set
  • Practical SAS Training Course
  • SAS Certified Specialist Training Program
  • Proc SQL Course
  • Introduction to Time Series Analysis
  • SAS Project Training Course
  • Full Training / Membership
  • Sign up
  • About us
  • Contact us
Sentry Page Protection
Please Wait...
SAS Functions [2-14]


SUM Functions

The SUM function allows you to add up values despite the present of missing values.

In SAS, there are two ways to add up numeric values: 

  1. Plus Sign (+) or
  2. SUM function

The usage of the Plus Sign (+) has been demonstrated in the previous session.
The downside of using the Plus Sign (+) is that the summation will not be performed with the present of missing values.

Let's illustrate this with an example.
Picture

The NUMBERS data set above contains 3 variables: Val0, Val1 and Val2. 

[Note: To see the NUMBERS data set on SAS Studio, run the code in the yellow box above]

Some values from the data set are clearly missing.

Now, we're going to add up all 3 variables using the Plus Sign (+). 

Example

Data Math1;
    Set Numbers;
    val3 = val0+val1+val2;
Run;
Picture

The numbers were not added up when there are missing values!

This is not good, as you could potentially lose tons of data
because of the present of just 1 missing value!

The better way to add up values is to use the SUM function.

Example

Data Math2;
    Set Numbers;
    Val3 = Sum(of val0, val1, val2);
Run;
Picture

The SUM function will ignore any missing values when adding up the numbers.

No data will be lost due to missing values.


Function Structure

The SUM function is structured as:

Sum(of val1, val2, ...);

You can have as many parameters in the SUM function as you want.

Exercise

Copy and run the SALES data set from the yellow box below.
Picture

SALES contains 6 variables:
  • STOREID
  • MON: SALES (Monday)
  • TUE: SALES (Tuesday)
  • WED: SALES (Wednesday)
  • THR: SALES (Thursday)
  • FRI: SALES (Friday)


Write a SAS program to calculate the total sales of the week. Create any data set or variables if necessary.
Next

Need some help? 


HINT:
The plus sign (+) will not work when there is missing value.


SOLUTION:
Data Sales_Week;
Set Sales;
Total = Sum(of Mon, Tue, Wed, Thr, Fri);
Run;


Fill out my online form.

Already a member? Go to member's area.