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=)
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.