GetObsCount.sas

/****************************************************************************/
/*  Program Name  : GetObsCount                                             */
/*  Purpose       : Return the number of observations in a given data set.  */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Author        : Matt Trzcinski                                          */
/*  Last Update   : 2016/12/16                                              */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Input(s)      : SAS dataset                                             */
/*  Output(s)     : Number of observations as string                        */
/*  Usage         : Cannot be called in open code.  Must be part of an      */
/*                  expression or statement.                                */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Example       : %put There are %GetObsCount(sashelp.cars) observations; */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Note          : Modified from http://support.sas.com/kb/24/671.html     */
/*                                                                          */
/****************************************************************************/
%macro GetObsCount(dataset);
  /* Open assigns ID to open data set.  Assigns 0 if DNE */
  %let exists = %sysfunc(open(&dataset));

  %if &exists %then
  %do;
    %let returnValue  = %sysfunc(attrn(&exists, nobs));
    %let closed       = %sysfunc(close(&exists));
  %end;
  /* Output error if no dataset */
  %else %put %sysfunc(sysmsg());

  &returnValue
%mend;

Powered by peut-publier

©2020 Matt Trzcinski