"Why" do you need a macro? Have you created code which does this in Base SAS, if so then provide it, and that can then be put into a macro. If you just want the number of observations from a dataset, then query the metadata tables (SASHELP.VTABLE, VCOLUMN, or DICTIONARY.TABLES, COLUMS in SQL). User24feb has given you the code for this type of thing.
You're asking for ready made code without putting in some own work first - and amazingly people in this forum work for you free of charge.
I believe it's now really up-to-you to spend a bit of time to understand the answers and use stuff as people tell you it needs to be used.
If you continue on the path you've taken then I start to think "troll".
Try:
Data A;
Do i=1 To 18;
Output;
End;
Run;
Proc SQL NoPrint;
* If a dataset is required use;
Create Table xx As Select Nobs From Dictionary.Tables
Where UpCase(Libname)='WORK' AND UpCase(Memname)='A' ;
* If you need a macro-variable;
Select Nobs Into :M_Nobs Trimmed From Dictionary.Tables
Where UpCase(Libname)='WORK' AND UpCase(Memname)='A' ;
Quit;
%Put ***&M_Nobs.*;
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);
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.