ListDatasetVariables.sas

/****************************************************************************/
/*  Program Name  : ListDatasetVariables                                    */
/*  Purpose       : Print dataset variables names to log as a vertical list */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Author        : Matt Trzcinski                                          */
/*  Last Update   : 2017/07/05                                              */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Input(s)      : Data set name                                           */
/*  Output(s)     : Log message                                             */
/*  Usage         : dataset= Data whose variables are to be listed.         */
/*                   number= (optional) Number each variable. Default is    */
/*                           false.                                         */
/*                    space= (optional) Include a blank line between each   */
/*                           variable in the output. Default is false.      */
/*                    type=  (optional) Include "$ &defLength." column in   */
/*                           the output. Default is false.                  */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Example(s)    : %ListDatasetVariables(sashelp.class);                   */
/*                                                                          */
/*                    Output                                                */
/*                    ------                                                */
/*                      Dataset: SASHELP.CLASS                              */
/*                                                                          */
/*                      Name                                                */
/*                      Sex                                                 */
/*                      Age                                                 */
/*                      Height                                              */
/*                      Weight                                              */
/*                                                                          */
/*                 %ListDatasetVariables(sashelp.class, number=true);       */
/*                                                                          */
/*                    Output                                                */
/*                    ------                                                */
/*                      Dataset: SASHELP.CLASS                              */
/*                                                                          */
/*                      1: Name                                             */
/*                      2: Sex                                              */
/*                      3: Age                                              */
/*                      4: Height                                           */
/*                      5: Weight                                           */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Note          :                                                         */
/*                                                                          */
/*  Developer     :                                                         */
/*                                                                          */
/****************************************************************************/

%macro IsEmpty(macroVariable);
  %sysevalf(%superq(&macroVariable)=, boolean)
%mend;

%macro SetSystemOptions(opt1, opt2, opt3);
options &opt1. &opt2. &opt3.;
%mend;

%macro ListDatasetVariables(dataset, number=false, space=false, type=false) / minoperator mindelimiter = ',';

  %let originalNOTES  = %sysfunc(getoption(notes));

  options nonotes;

********************************************************************

** Validate inputs

********************************************************************;

**********************************

*** Check for existance

**********************************;
  %if %IsEmpty(dataset) %then %do;
    %put ERROR: [MACRO] DATASET argument is blank.;
    %SetSystemOptions(&originalNOTES.);
    %abort cancel;
    %end;

  %if not %sysfunc(exist(&dataset.)) %then %do;
    %put ERROR: [MACRO] Dataset &dataset. could not be found.;
    %SetSystemOptions(&originalNOTES.);
    %abort cancel;
    %end;

  %if %IsEmpty(number) %then %do;
    %put ERROR: [MACRO] NUMBER argument is blank.;
    %SetSystemOptions(&originalNOTES.);
    %abort cancel;
    %end;

  %if %IsEmpty(space) %then %do;
    %put ERROR: [MACRO] SPACE argument is blank.;
    %SetSystemOptions(&originalNOTES.);
    %abort cancel;
    %end;

  %if %IsEmpty(type) %then %do;
    %put ERROR: [MACRO] TYPE argument is blank.;
    %SetSystemOptions(&originalNOTES.);
    %abort cancel;
    %end;

**********************************

*** Standardize input

**********************************;
  %let dataset  = %upcase(&dataset.);
  %let number   = %upcase(&number.);
  %let space    = %upcase(&space.);
  %let type     = %upcase(&type.);

**********************************

*** Check for TRUE/FALSE

**********************************;
  %if not ( &number. in (TRUE, FALSE) ) %then %do;
    %put ERROR: [MACRO] Invalid value [&number.] for NUMBER.;
    %SetSystemOptions(&originalNOTES.);
    %abort cancel;
    %end;

  %if not ( &space. in (TRUE, FALSE) ) %then %do;
    %put ERROR: [MACRO] Invalid value [&space.] for SPACE.;
    %SetSystemOptions(&originalNOTES.);
    %abort cancel;
    %end;

  %if not ( &type. in (TRUE, FALSE) ) %then %do;
    %put ERROR: [MACRO] Invalid value [&type.] for TYPE.;
    %SetSystemOptions(&originalNOTES.);
    %abort cancel;
    %end;

********************************************************************

** List variables

********************************************************************;

**********************************

*** Toggle type expression

**********************************;
  %if &type. = TRUE %then %let typeExpression = "$ %nrstr(&defLength.)";
  %else %let typeExpression=;

**********************************

*** Generate put statement

**********************************;
  %if       ( &number. = TRUE   and &space. = TRUE )  %then
    %let putStatement = put 'i' i +(-1) ': ' @7 variable @41 &typeExpression. /;
  %else %if ( &number. = FALSE  and &space. = TRUE ) %then
    %let putStatement = put variable @35 &typeExpression. /;
  %else %if ( &number. = TRUE   and &space. = FALSE ) %then
    %let putStatement = put 'i' i +(-1) ': ' @7 variable @41 &typeExpression.;
  %else %if ( &number. = FALSE  and &space. = FALSE ) %then
    %let putStatement = put variable @35 &typeExpression.;

**********************************

*** List variables

**********************************;
  data _null_;

    put;
    put "Dataset: &dataset.";
    put;

    identifier = open("&dataset.", 'i');
    number_of_variables = attrn(identifier, 'nvars');

    do i = 1 to number_of_variables;
      variable = varname(identifier, i);
      &putStatement.;
    end;

    rc = close(identifier);
  run;

**********************************

*** Housekeeping

**********************************;
  %SetSystemOptions(&originalNOTES.);

%mend;

Powered by peut-publier

©2020 Matt Trzcinski