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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1247 views
  • 0 likes
  • 3 in conversation