SAS MACRO

Reply
Contributor
Posts: 26

SAS MACRO

i have a macro which will gives the noSmiley Surprisedf obs and noSmiley Surprisedf 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=);

Super User
Posts: 9,676

Re: SAS MACRO

proc sql;

select memname,nvar,nobs

from dictionary.tables

  where libname='SASHELP';

quit;

Ksharp

Contributor
Posts: 26

Re: SAS MACRO

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

Super User
Posts: 9,676

Re: SAS MACRO

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

New Contributor
Posts: 4

Re: SAS MACRO

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;


Super User
Posts: 9,676

Re: SAS MACRO

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

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

..........

Contributor
Posts: 26

Re: SAS MACRO

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=)

Ask a Question
Discussion stats
  • 6 replies
  • 397 views
  • 0 likes
  • 3 in conversation