The 9.1 macro reference book contains a macro to find the number of obs and the number of variables. In fact, the attrn function can return a number of other attributes of a dataset. I found that it was not necessary to create a macro variable and that the macro call would act as a macro variable.

The nice thing about having this as a macro is that it can be used anywhere: data step, title statement, SQL query, etc. without problems with step boundary conditions.

%macro getnobs(dsname=);
%*---------------------------------------------------------------------------;
%* ;
%* Name: getnobs.sas ;
%* Author: Robert Drozd (RED) ;
%* Purpose: Return the number of obs in a dataset as either a number when ;
%* action=code otherwise as a NOTE:. ;
%* This method is the preferred method to find the number of obs ;
%* in a dataset since it can be used anywhere and does not run ;
%* or compile a proc or data step. ;
%*---------------------------------------------------------------------------;
%let dsnum=%sysfunc(open(&dsname));
%sysfunc(attrn(&dsnum,nlobs))
%let dsnum=%sysfunc(close(&dsnum));
%mend getnobs;

I placed a macro like this in our autocall macro library and it has served us well. The call to the macro say, %getnobs(dsname=sashelp.shoes) simply resolves to a string of 395. I try to use global macro vars only when necessary so I especially like this one.

Doesn't look like this technique spread very far. I wonder why.