I wonder if there is a way of detecting whether a data set is empty, i.e. it has no observations. Or in another saying, how to get the number of observations in a specific data set.
So that I can write an If statement to set some conditions.
Thanks.
The trick is producing an output even when the dataset is empty.
The above code is the simplest way I've found to produce the number of observations even when the dataset is empty. I've heard NOBS can be tricky, but the above can work for simple applications.
There are lots of different ways, I tend to use a macro function with
open()
andattrn()
. Below is a simple example that works great most of the time. If you are going to be dealing with data views or more complex situations like having a data set with records marked for deletion or active where clauses, then you might need more robust logic.Proc sql is not efficient when we have large dataset. Though using ATTRN is good method but this can accomplish within base sas, here is the efficient solution that can give number of obs of even billions of rows just by reading one row:
Here's the more complete example that @cmjohns was talking about. It will return 0 if it is empty, -1 if it is missing, and has options to handle deleted observations and where clauses (note that using a where clause can make the macro take a long time on very large datasets).
Usage Notes:
This macro will return the number of observations in a dataset. If the dataset does not exist then -1 will be returned. I would not recommend this for use with ODBC libnames, use it only against SAS tables.
Parameters:
libname.dataset
that you want to check.NOBS
ORNLOBSF
. See SASV9 documentation for descriptions.Macro definition:
Example Usage:
Results
Installation
I recommend setting up a SAS autocall library and placing this macro in your autocall location.
A slightly different approach:
This will give you a dataset with one observation; the variable nobs has the value of number of observations in the dataset, even if it is 0.
It's easy with PROC SQL. Do a count and put the results in a macro variable.