Published on :
Macro CREATION_INTERNE

WHEROBS Macro - Filtered Observation Count

This code is also available in: Deutsch Español Français
This macro determines the number of rows in a table. If no filter is specified, it delegates the counting to another macro named '%numobs'. If a filter is provided, it uses system functions (OPEN, ATTRN parameterized with 'NLOBSF', CLOSE) to obtain the number of logical observations respecting the condition, which is generally more performant than a DATA step or a SQL COUNT(*).
Data Analysis

Type : CREATION_INTERNE


The script only defines programming logic (macro) without accessing specific data during its compilation.

1 Code Block
MACRO
Explanation :
Definition of the 'wherobs' macro. It checks if the 'mywhere' parameter is empty. If so, it calls '%numobs'. Otherwise, it opens the table via %sysfunc(open(...)) with the filter, retrieves the number of filtered observations via %sysfunc(attrn(..., nlobsf)), and closes the table.
Copied!
1*m204d08b;
2 
3%macro wherobs(dsn,mywhere);
4 %IF %superq(mywhere)= %THEN %DO;
5 %numobs(&dsn)
6 %return;
7 %END;
8 %local dsid rc;
9 %let dsid=%sysfunc(open(&dsn(where=(&mywhere))));
10 %IF &dsid>0 %THEN %DO;
11 %sysfunc(attrn(&dsid,nlobsf))
12 %let rc=%sysfunc(close(&dsid));
13 %END;
14 %ELSE %put ERROR: Could not open dataset %upcase(&dsn).;
15%mend wherobs;
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.