  /*------------------------------------------------------------------*
   | MACRO NAME  : nobs
   | SHORT DESC  : Create a macro variable for the number of
   |               observations in a data set
   *------------------------------------------------------------------*
   | CREATED BY  : Unknown, Unknown              (04/08/2004 13:33)
   *------------------------------------------------------------------*
   | PURPOSE
   |
   | This macro creates a macro variable containing the number of
   | observations in a SAS dataset.  The following parameters are used on
   | the macro call:
   |
   |      lib=     libname of the SAS library containing your dataset.
   |               The default is WORK.
   |
   |      dsn=     name of the SAS dataset.  No default.
   |
   |      macvar=  name of the global macro variable containing the number
   |               of physical(includes deleted)observations.  Default is nobs.
   |      lmacvar= name of the global macro variable containing the number
   |               of logical(undeleted) observations.  Default is nlobs.
   | If you specify a non-existent libname or dataset name the macro
   | variable returns a blank which will probably cause an error when the
   | macro variable is referenced later in your program.
   |
   |
   *------------------------------------------------------------------*
   | MODIFIED BY : Kosanke, Jon                  (10/12/2009  9:58)
   |
   | Add a parameter to give the number of logical(undeleted) observations
   | on the dataset
   *------------------------------------------------------------------*
   | MODIFIED BY : Kosanke, Jon                  (10/14/2009 15:48)
   |
   | Declare working macro variables as local
   *------------------------------------------------------------------*
   | OPERATING SYSTEM COMPATIBILITY
   |
   | UNIX SAS v8   :   YES
   | UNIX SAS v9   :   YES
   | MVS SAS v8    :
   | MVS SAS v9    :
   | PC SAS v8     :   YES
   | PC SAS v9     :   YES
   *------------------------------------------------------------------*
   | Copyright 2009 Mayo Clinic College of Medicine.
   |
   | This program is free software; you can redistribute it and/or
   | modify it under the terms of the GNU General Public License as
   | published by the Free Software Foundation; either version 2 of
   | the License, or (at your option) any later version.
   |
   | This program is distributed in the hope that it will be useful,
   | but WITHOUT ANY WARRANTY; without even the implied warranty of
   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   | General Public License for more details.
   *------------------------------------------------------------------*/
 
%macro nobs(lib=WORK,dsn=,macvar=nobs,lmacvar=nlobs);
 %local notes error n nl;
 %global &macvar &lmacvar;
   proc sql;
    reset noprint;
    select setting into :notes from dictionary.options
       where optname='NOTES';
    quit;
   options nonotes;
   %let lib=%upcase(&lib);
   %let dsn=%upcase(&dsn);
   %let error=0;
   %let &macvar= ;
   %let &lmacvar= ;
   %let n= ;
   %let nl= ;
   %if &dsn= %then %do;
      %put ERROR: NO DATASET SPECIFIED.;
      %let error=1;
   %end;
   %if &error=0 %then %do;
      proc sql;
         reset noprint;
         select nobs as n format=best21., nobs-delobs as nl format=best21. into :n, :nl from dictionary.tables
         where libname="&lib" & memname="&dsn";
      quit;
      %if &n= %then %do;
         %put ERROR: DATASET NOT FOUND.;
      %end;
      %else %do;
         %let &macvar=&n;
         %let &lmacvar=&nl;
      %end;
   %end;
   options &notes;
%mend nobs;
