BookmarkSubscribeRSS Feed
supersonic
Calcite | Level 5

i have a macro which will gives the no:of obs and no:of vars of a sasdata set

but my aim is to  take care of all the datasets in a library(without manualy giving the dataset names).

please help.

%macro obsnvars(lib=,ds=);
   %global dset nvars nobs;

   %let dset=&lib..&ds;
   %let dsid = %sysfunc(open(&dset));

   %if &dsid %then
      %do;
         %let nobs =%sysfunc(attrn(&dsid,nobs));       

         %let nvars=%sysfunc(attrn(&dsid,nvars));
         %let rc = %sysfunc(close(&dsid)); 

         %put &dset has &nvars  variable(s) and &nobs observation(s).;
      %end;
   %else

%mend  obsnvars;

% obsnvars(lib=,ds=);

6 REPLIES 6
Ksharp
Super User

proc sql;

select memname,nvar,nobs

from dictionary.tables

  where libname='SASHELP';

quit;

Ksharp

supersonic
Calcite | Level 5

My aim is to create a macro that looks at all the datasets in a library, counts their number of observations and number of columns and puts them into macro variables..

Ksharp
Super User

OK.

%macro lib(lib=);
proc sql noprint;
select count(*) into : n 
 from dictionary.members
  where libname="%upcase(&lib)";
select memname,nvar,nobs
 into :     m1-: m%left(&n), :     v1-: v%left(&n), :     o1-: o%left(&n)
 from dictionary.tables
  where libname="%upcase(&lib)";
quit;
%put _user_;
%mend lib;

%lib(lib=work)


Ksharp

SAS333
Calcite | Level 5

Hello,

Below code extracts all the SAS datasets and the number of observations and variables in each dataset.

%macro obsnvars(lib=);

%if &lib= %then %let lib=WORK;

%else %let lib=%upcase (&lib);

proc sql;

select memname,nobs,nvar from dictionary.tables where libname="&lib" and memtype="DATA" and memname not in ('_PRODSAVAIL');

quit;

%mend obsnvars;


Ksharp
Super User

There is no need to add %if condition, just add WORK into arguments.

%macro obsnvars(lib=WORK);
proc sql;

..........

supersonic
Calcite | Level 5

thank you Ksharp and SAS333

%macro lib(lib=);

proc sql noprint;
select memname,nvar,nobs
into :     m1, :     v1, :     o1
from dictionary.tables
  where libname="%upcase(&lib)" and memtype="DATA";
quit;

%mend lib;

%lib(lib=)

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—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
  • 2337 views
  • 0 likes
  • 3 in conversation