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=);
proc sql;
select memname,nvar,nobs
from dictionary.tables
where libname='SASHELP';
quit;
Ksharp
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..
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
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;
There is no need to add %if condition, just add WORK into arguments.
%macro obsnvars(lib=WORK);
proc sql;
..........
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=)
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.