Try the second link for starters. The full code is available for the macro.
Before creating a macro you need to know how to do it without a macro.
As for your question - next code will select all datasets in a given library and their number of observations:
data want;
set sashelp.vtable(where=(libname=<reqired LIBRARY in uppercase>));
keep memname nobs;
run;
/** memname=Member Name or table name **/
/** nobs = No Of Observations **/
There are many ways to convert it into macro programing. For example:
%LET lib = MYDATA;
data want;
set sashelp.vtable(where=(libname="&lib"));
keep memname nobs;
run;
OR:
%macro list(lib);
data want;
set sashelp.vtable(where=(libname="&lib"));
keep memname nobs;
run;
%mend list;
%list(MYLIB);
and there are more methods - as using SQL dictionary.
Try the second link for starters. The full code is available for the macro.
Do you only want the number of observations? Do you want them labeled in any way? There's a lot of interpretation that we have to do from your question. The below example concatenates all the data sets in the SASHELP library and their respective observations into a macro variable called "observations":
proc sql noprint;
select
catx("-", memname, nobs)
into: observations separated by ", "
from
dictionary.tables
where
libname = "SASHELP" and
memtype = "DATA";
quit;
%put &observations;
We need more information though. Just number of observations into separate macro variables would be the following:
proc sql noprint;
select
nobs
into: observations1 -
from
dictionary.tables
where
libname = "SASHELP" and
memtype = "DATA";
quit;
%put _user_;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.