Table of Contents
Data cleaning and preparation are arguably the most crucial stages of any successful statistical analysis. A common task in this process involves identifying and isolating observations where key variables are present, rather than absent or missing. In the SAS statistical software environment, the efficient selection of observations that are not considered null values is paramount for generating reliable reports and analyses.
To achieve this filtering, SAS provides robust tools, primarily centered around the WHERE clause. This powerful clause allows users to specify conditions that observations must meet to be included in the resulting output or dataset. When dealing with missing values, the condition specified typically involves checking if a variable is not equal to a missing indicator, frequently accomplished using the built-in MISSING keyword or the MISSING() function, particularly when using PROC SQL.
Understanding Missing Data and Null Values in SAS
It is critical to understand how SAS differentiates missing values based on variable type. For numeric variables, a missing value is represented by a single period (.). For character variables, a missing value is represented by a blank space (or a string of blanks, depending on length). This distinction slightly influences how you phrase the filtering condition, although the MISSING() function is designed to handle both types consistently, offering a unified approach to data quality checks.
When selecting non-null observations, we are essentially asking SAS to include only records where the specified variable contains valid, non-missing data. Excluding missing data ensures that calculations, such as means, sums, or regression coefficients, are based on complete information, thereby increasing the integrity and validity of the final analytical output.
While traditional SAS programming often uses the `WHERE variable NE .` syntax for numeric variables (NE stands for “not equal”), utilizing the more general and explicit NOT MISSING(variable) function provides greater clarity and applicability across different data types, especially within the powerful framework of SQL processing.
Utilizing the WHERE Clause for Efficient Filtering
The WHERE clause is a fundamental component of both the SAS Data Step and various SAS procedures like PROC PRINT, PROC REPORT, and especially PROC SQL. Its purpose is to subset data based on logical criteria. To select observations that are not null, you must negate the condition for missingness.
For instance, in a traditional Data Step, if you have a numeric variable named score, you could write: WHERE score IS NOT MISSING; or the equivalent WHERE score NE .;. Both statements instruct SAS to process only those records where score has an assigned value. If score were a character variable, you would use WHERE score NE ' ';, though this requires careful handling of potential leading or trailing blanks. This is why the use of the generalized MISSING function within procedures like PROC SQL is often preferred, as it abstracts away the specific data type handling required for missingness.
The Power of PROC SQL: Filtering Non-Missing Observations
When working with large datasets or when integrating SAS processing with standard database management techniques, PROC SQL offers a highly flexible and familiar structure for data manipulation. In the context of selecting non-null observations, PROC SQL leverages the WHERE clause combined with the MISSING() function to achieve efficient filtering.
The standard syntax involves using the logical operator NOT in conjunction with the MISSING function. The MISSING(variable) function returns a true (1) value if the specified variable is missing, and a false (0) value otherwise. By preceding this with NOT, we select only those rows where the function returns false—i.e., where the variable is not missing.
This approach provides a clean and highly readable way to handle data exclusion based on completeness. You can use the following basic syntax to select observations in a dataset in SAS where a certain column value is not null:
You can use the following basic syntax to select observations in a dataset in SAS where a certain column value is not null:
/*select only rows where var1 is not null*/
proc sql;
select *
from my_data1
where not missing(var1);
quit;This structure is highly recommended due to its clear semantic meaning and consistency across variable types, making your code robust and easy to maintain.
Practical Example: Setting Up the Dataset
To illustrate this filtering technique, we will first create a sample dataset containing both complete and missing observations. This synthetic data will represent team performance, where some point totals have been recorded as missing (indicated by the period . for numeric variables in SAS). Note that the team name (team) is a character variable, while the score (points) is numeric.
This setup allows us to clearly observe which rows are retained and which are excluded when we apply the non-null filtering logic to the points column. The following code demonstrates the creation of the initial dataset, my_data1, followed by a procedural step to view its contents before filtering.
Example: Select Observations Which are Not Null in SAS
Suppose we have the following dataset in SAS:
/*create dataset*/
data my_data1;
input team $ points;
datalines;
A 15
B .
C 22
D 19
E 29
F .
G 40
H 35
;
run;
/*view dataset*/
proc print data=my_data1;
Once this code is executed, the resulting output will clearly show the presence of missing data within the points column, confirming the need for a targeted selection process.

As you can observe, records for Team B and Team F have null values in the points column, represented by the period (.). Our goal is to isolate only those teams for which point totals are available.
Implementing the Non-Null Selection Logic
We are now ready to apply the filtering logic using PROC SQL. We will use the SELECT * statement to retrieve all columns, and the WHERE clause will incorporate the NOT MISSING(points) condition. This condition ensures that only records where the points variable is definitively recorded are included in the output table.
The use of the MISSING function here is crucial, as it provides a standardized way to check for system-missing values regardless of whether the underlying data structure is numeric or character. By using the logical negation NOT, we effectively create a robust filter against data incompleteness.
We can use the following code to select all of the rows where the value in the points column is not null:
/*select only rows where points is not blank*/
proc sql;
select *
from my_data1
where not missing(points);
quit;Executing this code will produce a result table that is a clean subset of the original dataset, containing only the six teams for which scores were available.

As demonstrated by the output, only the rows where the value in the points column is not null are returned. The observations corresponding to Teams B and F have been successfully excluded from the result set, validating the efficiency of the WHERE NOT MISSING() syntax within PROC SQL.
Advanced Techniques: Counting Non-Missing Records
Beyond simply subsetting the data, analysts often need to quickly determine the volume of complete records available for a specific variable. This is especially useful for quality control checks and determining sample size sufficiency. PROC SQL facilitates this efficiently by combining the filtering logic with aggregate functions, such as COUNT().
By applying the same WHERE clause condition (WHERE NOT MISSING(points)) to a SELECT COUNT(*) query, we instruct SAS to count all rows in the my_data1 table that meet the criterion of having a non-null value in the points column. This avoids the need to first subset the data and then count the resulting observations.
Note that you could also use the count() function in proc sql to count the number of observations where the value in the points column is not null:
/*count rows where points is not blank*/
proc sql;
select count(*)
from my_data1
where not missing(points);
quit;This method provides a highly optimized way to derive summary statistics regarding data completeness directly within the SQL environment.
![]()
The result, 6, tells us that 6 observations in the dataset have a value that is not null in the points column. This confirms our manual count and demonstrates the accuracy and utility of combining the COUNT() function with the non-missing filter.
Best Practices for Data Integrity in SAS
Ensuring data integrity by proactively handling null values is a non-negotiable step in serious data processing. While the NOT MISSING() technique is highly effective for filtering, analysts should also consider best practices when dealing with missing data:
- Consistency: Always use the
MISSING()function or the explicit period (.) for numeric data, rather than relying on complex comparison operators which might introduce ambiguity. - Documentation: Clearly document your choice of exclusion criteria. If you are filtering out null values, state which variables are subject to this criterion.
- Multiple Variables: If you need to select observations where multiple variables are non-null, use logical operators (
ANDorOR) within the WHERE clause. For example:WHERE NOT MISSING(var1) AND NOT MISSING(var2);. - Data Step Alternatives: For situations requiring complex conditional logic or iterative processing, the Data Step offers the
IF N(variable) > 0syntax for numeric variables, which is a shorthand way of checking for non-missing values (since the N function returns the number of non-missing arguments).
By mastering the use of the WHERE clause and the MISSING function within PROC SQL, SAS users can ensure their analyses are based on clean, complete, and reliable data subsets.
