How to count missing values in SAS (with examples)

In SAS, missing values can be counted by using the NMISS function. This function counts the number of missing values in a single variable or multiple variables in a dataset. For example, the code NMISS(of var1-var5) will count the missing values in variables var1-var5, and NMISS(var1) will count the missing values in variable var1. The output will give the total number of missing values for each variable. Additionally, the NMISS function can be used within code statements to make decisions, such as IF NMISS(var1)> 0 THEN statement. This statement will execute the code statement only if there are missing values in the variable var1.


You can use the following methods to count the number of missing values in SAS:

Method 1: Count Missing Values for Numeric Variables

proc means data=my_data
    NMISS;
run;

Method 2: Count Missing values for Character Variables

proc sql; 
    select nmiss(char1) as char1_miss, nmiss(char2) as char2_miss
    from my_data;
quit;

The following examples show how to use each method in practice with the following dataset in SAS:

/*create dataset*/
data my_data;
    input team $ pos $ rebounds assists;
    datalines;
A G 10 8
B F 4 .
. F 7 10
D C . 14
E F . 10
F G 12 7
G C . 11
;
run;

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

Example 1: Count Missing Values for Numeric Variables

We can use the following code to count the number of missing values for each of the numeric variables in the dataset:

/*count missing values for each numeric variable*/
proc means data=my_data
    NMISS;
run;

From the output we can see:

  • There are 3 total missing values in the rebounds column.
  • There is 1 total missing value in the assists column.

Example 2: Count Missing Values for Character Variables

We can use the following code to count the number of missing values for each of the character variables in the dataset:

/*count missing for each character variable*/
proc sql; 
    select nmiss(team) as team_miss, nmiss(pos) as pos_miss
    from my_data; 
quit;

From the output we can see:

  • There is 1 missing value in the team column.
  • There are 0 missing values in the pos column.

Note: You can find the complete documentation for the NMISS function .

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

x