Here's a macro similar to @AbhiD. If you don't specify a data set in the macro call, it will print to the log saying no data set specified (1st call). If you specify a data set that does not exist, it will print ot the log saying the data set does not exist (2nd call). If you specify an existing data set, it will print to the log the number of observations in that data set (3rd call). I'm not sure what you're looking to do with the number of observations, so you may want to be more specific. For example, if you want the number of observations in a macro variable or a data set, or if you just want it printed to the log (which is what this will do):
%macro nobs(dsn);
%if &dsn= %then %do;
%put *****************************;
%put *** No Data Set Specified ***;
%put *****************************;
%end;
%else %do;
%let dsid=%sysfunc(open(&dsn));
%if &dsid=0 %then %do;
%put *******************************;
%put *** Data Set Does Not Exist ***;
%put *******************************;
%end;
%else %do;
%let obs=%sysfunc(attrn(&dsid,nlobs));
%put ****************************************************;
%put *** Number of Observations in Data Set is = &obs ***;
%put ****************************************************;
%end;
%end;
%mend;
%nobs();
%nobs(data);
data test;
do i=1 to 10;
output;
end;
run;
%nobs(test);
... View more