Wednesday, October 18, 2006

Using The SUM Function to Prevent Missing Values

You can use the SUM function to prevent missing values if one argument has a missing value.

The syntax for using the SUM function to create a new variable is:
newvar=sum(var1, var2);
The SUM function ignores missing values, so if var1 and var2 are both missing values then the value of newvar will also be missing.

You can prevent that by coding in a constant zero:
newvar=sum(var1, var2, 0);

In the following example:
1. A data set is created by reading in three variables: var1, var2 and var3.
Note that the third observation has missing values for var1 and var2.

2. The SUM function is used to create newvar1, which is the sum of var1 and var2.

3. The SUM function is also used to create newvar2 by summing var1 and var2, but it also codes in a constant zero.

data step1;
input @1 var1 1-2
@4 var2 4-5
@7 var3 7-8
;
newvar1=sum(var1, var2);
newvar2=sum(var1, var2,0);
datalines;
10 20 15
10 10 25
35
20 20 45
;
run;

proc print data=step1;
var var1 var2 newvar1 newvar2;
run;
quit;


For the third observation, there is a missing value for newvar1. But by including a zero in the SUM statement for newvar2, the value of the third observation is zero instead of missing.

0 Comments:

Post a Comment

<< Home