%macro getnobs(set, /* sas data set for which the obs count is to be returned*/ display=0, /* specify display=1 to generate a display of the "answer"*/ nobsvar=nobs /* specifies the name of the global parm (sas macro variable) where the obs count will be assigned. Default is to have it assigned to &nobs. */ /* Usage note: if value cannot be determined returned value is -999 */ ); %*--Coded by J. Blodgett, UMSL UIC, 4-21-97--; %*--This macro uses information in the sashelp.vtable (view of dictionary. tables) to retrieve the # of observations in a sas data set. It will by default assign the "answer" to the global parm "nobs". You can override this default by specifying "nobsvar=" when invoking the macro. Invoke with no parameters to get printed help.--; %if %bquote(&set) eq %str() %then %do; %*--Display help-----------; %put The getnobs macro uses the special "dictionary.tables" set to ; %put retrieve the # of observations in a sas dataset. The first ; %put and only (postional) parm passed to the macro names the sas ; %put data set. Specify the optional parm "display=1" to have the ; %put value of nobs displayed to the log. If it cannot find the ; %put data set, or if the process fails for any reason a value of ; %put -999 is returned. The value is returned as the output of the ; %put macro invocation. It must be invoked on a step boundary since; %put it generates a proc (sql) step. ; %put Examples: ; %put Ex. 1; %put %str (data x; set stf903x.moi; where slvl='050'; run;); %put %nrstr ( %getnobs(x) *--assigns obs count to global var nobs-;); %put %str( ); %put Ex 2; %put %nrstr (%getnobs(sasuser.crime,display=1,nobsvar=ncrime) ); %put %str ( Above assigns # obs in sasuser.crime set to global var ncrime.); %put %str (); %put %str ( ------------------------); %goto endmac; %end; %*<---of help---; %global &nobsvar; %let &nobsvar=-999; %*<---indicates a missing value--; %local libref memname; %let memname=%QSCAN(&set,2,%str(.)); %if %bquote(&memname) eq %then %do; %let libref=work; %let memname=&set; %end; %else %do; %let libref=%qscan(&set,1,%str(.)); %end; PROC SQL NOPRINT; SELECT NOBS INTO :&NOBSVAR from SASHELP.VTABLE WHERE libname=%upcase("&libref") AND memname=upcase("&memname"); quit; %if &&&nobsvar = -999 %then %put %str(*****getnobs unable to assign obs count for &set*****); %else %if &display %then %put &nobsvar %str( assiged value of &&&nobsvar ) %str(--# of observations in set ) &set; %endmac: %mend getnobs;