How to Replace Missing Values with Zero in SAS?

In SAS, missing values can be replaced with a zero by using the ‘input’ function with the ‘missover’ option. The ‘missover’ option will replace any missing values with a zero when a dataset is input into SAS. The ‘missover’ option must be used with caution in order to avoid skewing data.


Often you may want to replace missing values in a SAS dataset with zeros.

Fortunately this is easy to do using a simple if then statement.

The following examples show how to replace missing values with zeros in practice.

Example 1: Replace Missing Values in All Columns

Suppose we have the following dataset in SAS with three columns, each with some missing values:

/*create dataset*/
data my_data;
    input x y z;
    datalines;
1 . 76
2 3 .
2 3 85
4 5 88
2 2 .
1 2 69
5 . 94
4 1 .
. . 88
4 3 92
;
run;

/*view dataset*/
proc print data=my_data;

We can use the following code to replace the missing values with zeros in every column of the dataset:

/*create new dataset with missing values replaced by zero*/
data my_data_new;
   set my_data;
   array variablesOfInterest _numeric_;
   do over variablesOfInterest;
      if variablesOfInterest=. then variablesOfInterest=0;
   end;
run;

/*view new dataset*/
proc print data=my_data_new;

Notice that the missing values in each column have been replaced with zeros.

Note: The argument _numeric_ tells SAS to replace the missing values with zeros in every numeric column in the dataset.

Example 2: Replace Missing Values in Specific Column

Once again suppose we have the following dataset in SAS with three columns, each with some missing values:

/*create dataset*/
data my_data;
    input x y z;
    datalines;
1 . 76
2 3 .
2 3 85
4 5 88
2 2 .
1 2 69
5 . 94
4 1 .
. . 88
4 3 92
;
run;

/*view dataset*/
proc print data=my_data;

We can use the following code to replace the missing values with zeros in only the “y” column of the dataset:

/*create new dataset with missing values in "y" column replaced by zero*/
data my_data_new;
   set my_data;
   array variablesOfInterest y;
   do over variablesOfInterest;
      if variablesOfInterest=. then variablesOfInterest=0;
   end;
run;

/*view new dataset*/
proc print data=my_data_new;

Notice that only the missing values in the “y” column have been replaced with zeros.

The following tutorials explain how to perform other common tasks in SAS:

x