How to Use the MISSING Function in SAS (With Examples)


You can use the MISSING function in SAS to check if a variable contains a missing value.

This function uses the following syntax:

MISSING(expression)

where:

  • expression: The name of a character or numeric variable

This function will return 0 if the variable does not contain a missing value or 1 if it does contain a missing value.

The following example shows how to use this function in practice.

Example: How to Use the MISSING Function in SAS

Suppose we have the following dataset in SAS that contains information about various basketball players:

/*create dataset*/
data my_data;
    input team $ position $ points assists;
    datalines;
A Guard 14 4
A Guard 22 6
A Guard 24 9
A Forward 13 8
A Forward 13 9
A . 10 5
B Guard 24 4
B Guard . 6
B Forward 34 2
B Forward 15 5
B Forward 23 5
B . 10 4
;
run;

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

We can create a new dataset and use the MISSING function to create a variable that checks if each row in the position column is missing a value or not:

/*create new dataset*/
data new_data;
    set my_data;
    missing_position = missing(position);
run;

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

SAS MISSING function example

The new column called missing_position contains a value of 0 if there is no missing value in the position column and a value of 1 if there is a missing value.

Note: Although row 8 has a missing value in the points column, the missing_position column contains a value of 0 because there is no missing value in the position column.

Also note that you can use an IF ELSE function with the MISSING function to return values other than 0 and 1.

For example, the following code shows how to return “yes” or “no” instead:

/*create new dataset*/
data new_data;
    set my_data;
    if missing(position) then missing_position = 'yes';
    else missing_position = 'no';
run;

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

The new column called missing_position contains a value of no if there is no missing value in the position column and a value of yes if there is a missing value.

Note: You can find the complete documentation for the SAS MISSING function here.

Additional Resources

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

How to Count Missing Values in SAS
How to Replace Missing Values with Zero in SAS
How to Remove Rows with Missing Values in SAS

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *