BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BrahmanandaRao
Lapis Lazuli | Level 10
How to count observations for each dataset in a library using macros
1 ACCEPTED SOLUTION

Accepted Solutions
6 REPLIES 6
Shmuel
Garnet | Level 18

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.

Reeza
Super User
sashelp.vtable is a view of dictionary.table
BrahmanandaRao
Lapis Lazuli | Level 10
Thank you for your solution
Reeza
Super User

https://communities.sas.com/t5/forums/searchpage/tab/message?advanced=false&allow_punctuation=false&...

 

Try the second link for starters. The full code is available for the macro.

maguiremq
SAS Super FREQ

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_;
BrahmanandaRao
Lapis Lazuli | Level 10
Thank you so much

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1676 views
  • 0 likes
  • 4 in conversation