Published on :
Macro MIXTE

Macro to get the number of observations

This code is also available in: Deutsch Español Français
The `%nobs` macro takes one parameter, `dset`, which represents the name of a SAS© dataset. It uses the SAS© `%SYSFUNC` functions to open the dataset (`OPEN`), retrieve its number of observations (`ATTRN` with the 'nobs' attribute), and then close the dataset (`CLOSE`). The value of the number of observations is directly returned by the macro.
Data Analysis

Type : MIXTE


The macro is generic and can be applied to any valid SAS dataset, whether it is created internally, comes from default SAS libraries like SASHELP (as in the commented example), or from external sources. It does not handle the creation or management of source data, but operates on existing data.

1 Code Block
MACRO nobs
Explanation :
This block defines the `%nobs` macro. It uses `%sysfunc(open(&dset))` to obtain a dataset identifier, then `%sysfunc(attrn(&dsid,nobs))` to extract the number of observations. The number is then displayed (`&nobs`), and the dataset is closed by `%sysfunc(close(&dsid))`. The commented lines are examples of use and do not affect the macro definition.
Copied!
1%macro nobs(dset) ;
2 %let dsid=%sysfunc(open(&dset)) ;
3 %let nobs=%sysfunc(attrn(&dsid,nobs)) ;
4 &nobs
5 %let dsid=%sysfunc(close(&dsid)) ;
6%mend nobs ;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Copyright Astellas Europe B.V, Macro name: nobs.sas, Version: 001, Author: Phil Mason, Date: 1Sep2008, SAS version: 9.1.3 SP4