The code below can be used to store the number of observations from a data set in a macro variable, &NUM.
%let dsid=%sysfunc(open(dataset_name));
%let num=%sysfunc(attrn(&dsid,nobs));
%let rc=%sysfunc(close(&dsid));
In some cases &NUM may resolve to a non-zero value even if there are 0 observations in the data set. This is due to the way the attribute treats observations marked for deletion and active WHERE clauses.
The NOBS attribute counts the number of physical observations including those marked for deletion. If a non-zero value is returned when 0 is expected, consider using NLOBS or NLOBSF instead. NLOBS counts the number of logical observations and does not include those marked for deletion. NLOBSF is the same as NLOBS and also takes the FIRSTOBS system option, the OBS system option, and the WHERE clauses into account.
%let num=%sysfunc(attrn(&dsid,nlobs));
%let num=%sysfunc(attrn(&dsid,nlobsf));