GetVarCount.sas

/****************************************************************************/
/*  Program Name  : GetVarCount                                             */
/*  Purpose       : Return the number of variables in a given data set.     */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Author        : Matt Trzcinski                                          */
/*  Last Update   : 2017/01/10                                              */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Input(s)      : SAS dataset                                             */
/*  Output(s)     : Number of variables as string                           */
/*  Usage         : Cannot be called in open code.  Must be part of an      */
/*                  expression or statement.                                */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Example       : %put There are %GetVarCount(sashelp.cars) variables;    */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Note          : Modified from http://support.sas.com/kb/24/671.html     */
/*                                                                          */
/****************************************************************************/
%macro GetVarCount(dataset);
  %let dsExists = %sysfunc(open(&dataset));
  %if &dsExists %then
  %do;
    %let varCount  = %sysfunc(attrn(&dsExists, nvars));
    %let closed       = %sysfunc(close(&dsExists));
    %end;
  %else %put %sysfunc(sysmsg());
  &varCount
%mend;

Powered by peut-publier

©2020 Matt Trzcinski