# Computing Means and Sums of Variables within an Observation. Counting the Number of Non-Missing Values in a List of Variables - SAS Programming

In this example,each subject answered 50 items in a psychological survey. Each question is scored on a 1 to 5 scale.To ensure that an accurate assessment is made, the mean score is computed only if 40 or more items were answered.Your job (should you choose to accept it) is to compute the mean of the 50 scores (ITEM1-ITEM50), but only if at least 40 items are nonmissing. Here is the program:

Example

DATA NEWTEST;
SET QLDTEST;
IF N (OF ITEM1-ITEM5Q) GE 40 THEN
SCORE*MEAN (OF ITEM1-ITEM50);
RUN;

As you probably figured out by inspecting this program, the N function returns the number of non-missing values in the variable list, and the MEAN function returns the mean of the nonmissing values in the variable list.Actually, you may not have figured out the importance of the way the MEAN function deals with non-missing values, and this is quite important.If you calculate SCORE by simply adding up all the items and then dividing by 50 as follows:

SCORE=(ITEM1 + ITEM2 + ITEM3 + ... + ITEM50)/50;

you would be in big trouble if any of the items had missing values. When a SAS statement tries to do arithmetic operations on missing values, the result is always missing.Countless hours have been (and undoubtedly will be) spent trying to figure out why seemingly perfect code continues to yield empty results.Now you know why.

You also know that the MEAN function computes the mean of the non-missing values and, therefore,always yields a valid result (unless of course all the data values are missing).In addition to the N and MEAN functions, the functions NMISS and SUM operate in a similar manner, with NMISS returning the number of variables with missing values and SUM returning the sum of the non-missing values.

The complementary pair of functions MIN and MAX are also useful and, as you would expect, return the minimum and maximum values in the argument list respectively.