How to Replace Missing Values with Zero in SAS


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.

Additional Resources

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

How to Normalize Data in SAS
How to Remove Duplicates in SAS
How to Use Proc Summary in SAS
How to Select Observations Which are Not Null in SAS

Leave a Reply

Your email address will not be published.