CreateDatasetListing.sas

/****************************************************************************/
/*  Program Name  : CreateDatasetListing                                    */
/*  Purpose       : Create well formatted listing for a given data set      */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Author        : Matt Trzcinski                                          */
/*  Last Update   : 2017/08/04                                              */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Input(s)      :                                                         */
/*                  title       = (Optional) Title (i.e. study name)        */
/*                  dataset     = Dataset to create listing for.            */
/*                  dirOut      = (Optional) Output path. Default is        */
/*                                C:\Users\&SYSUSERID\Documents             */
/*                  orientation = (Optional) Portrait or landscape. Default */
/*                                is portrait.                              */
/*                                                                          */
/*  Output(s)     : title - DATASET.rtf or DATASET.rtf                      */
/*                                                                          */
/*  Usage         : %CreateDatasetListing(                                  */
/*                            title=                                        */
/*                    ,     dataset=                                        */
/*                    ,      dirOut=                                        */
/*                    , orientation=                                        */
/*                    ,       style=                                        */
/*                  );                                                      */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Example(s)    :                                                         */
/*                                                                          */
/*                  %CreateDatasetListing(Study 123456                      */
/*                                      , sashelp.us_data                   */
/*                                      , "C:\Users\&SYSUSERID\Documents"   */
/*                   );                                                     */
/*                                                                          */
/*                                                                          */
/*                  %CreateDatasetListing(    dataset= sashelp.baseball     */
/*                                      , orientation= portrait             */
/*                  );                                                      */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Note          : Accepts both quoted and unquoted arguments for          */
/*                  title and dirOut.                                       */
/*                                                                          */
/*  Developer     : Create custom style template in a future version.       */
/*                  Currently, the MONOSPACE template is being used.        */
/*                                                                          */
/*                  Revise with %SetSytemOptions controlling system options.*/
/*                  Technically, this also clears title1, title2, and       */
/*                  footnote1.  Titles/footnotes are stored in              */
/*                  dictionary.titles. Something similar to                 */
/*                  %SetSystemOptions could be set up to preserve titles    */
/*                                                                          */
/****************************************************************************/

%macro CreateDatasetListing(title=
                          , dataset=
                          , dirOut='C:\Users\&SYSUSERID\Documents'
                          , orientation=PORTRAIT
                          , style=monospace
      ) / minoperator ;
%put NOTE: [MACRO] Executing: CreateDatasetListing(title=&title, dataset=&dataset, dirOut=&dirOut, orientation=&orientation);
%put;

  %let startTime = %sysfunc(datetime());

  /*Validate inputs*/
  %if %sysevalf(%superq(dataset)=, boolean) %then %do;
    data _null_;
      put 'ERROR: [MACRO] Missing parameter DATASET.';
      abort cancel;
    run;
  %end;

  %if not %sysfunc(exist(&dataset)) %then %do;
    data _null_;
      put "ERROR: [MACRO] Dataset %upcase(&dataset.) does not exist.";
      abort cancel;
    run;
  %end;

  %if not(%upcase(&orientation) in (LANDSCAPE PORTRAIT)) %then %do;
    data _null_;
      put "ERROR: [MACRO] Invalid orientation: &orientation.";
      abort cancel;
    run;
  %end;

  filename dirCheck "&dirOut.";
  %if not %sysfunc(fexist(dirCheck)) %then %do;
    data _null_;
      put "ERROR: [MACRO] Invalid directory &dirOut";
      option nonotes;
      filename dirCheck clear;
      option notes;
      abort cancel;
    run;
  %end;
  %else %do;
    option nonotes;
    filename dirCheck clear;
    option notes;
  %end;

  /*Remove quotes from arguments*/
  %if not %sysevalf(%superq(title)=, boolean) %then %do;
    %let title  = %sysfunc(dequote(&title));
    %end;
  %let dirOut = %sysfunc(dequote(&dirOut));

  /*Set output file name depending on whether

    title is given*/
  %if not %sysevalf(%superq(title)=, boolean) %then %do;
    %let fileName = &title - %upcase(&dataset);
    %end;
  %else %let fileName = %upcase(&dataset.);

  /*Save current SAS session options*/
  %let original_number      = %sysfunc(getoption(number));
  %let original_date        = %sysfunc(getoption(date));
  %let original_label       = %sysfunc(getoption(label));
  %let original_center      = %sysfunc(getoption(center));
  %let original_orientation = %sysfunc(getoption(orientation));

  /*Set options for good looking output*/
  options
    nonumber
    nodate
    nolabel
    center
    orientation = &orientation.
  ;

  /*Suppress listing*/
  ods listing close;
  /*Set output to rtf*/
  ods rtf file = "&dirOut.\&fileName..rtf" style=&style.;
  /*Prevent output file from automatically opening*/
  ods noresults;
  options nonotes;

  %if not %sysevalf(%superq(title)=, boolean) %then %do;
    title "&title.";
  %end;
  title2    "Dataset: &dataset.";
  footnote  'Page (*ESC*){thispage} of (*ESC*){lastpage}';

    proc print
      heading = horizontal
      width   = full
      split   = '_' /*If variables have _ separating words, then this splits into separate words*/
      data    = &dataset.
      ;
    run;

  /*Restore system settings*/
  title;
  title2;
  footnote;

  ods rtf close;
  ods results;
  ods listing;
  options notes;

  options
    &original_number.
    &original_date.
    &original_label.
    &original_center.
    orientation = &original_orientation.
  ;

  /*Time keeping*/
  %put NOTE: [MACRO] CREATEDATASETLISTING macro used (Total process time):;

  %let duration = %sysfunc(putn(%sysevalf(%sysfunc(datetime())-&startTime.), time12.3));
  %if %sysfunc(minute("&duration."t)) > 0 %then %do;
    %put NO%str(TE-)         real time            %substr(&duration., 3, 8);
  %end;
  %else %do;
    %put NO%str(TE-)         real time            %substr(&duration., 6, 5) seconds;
  %end;
%mend;

Powered by peut-publier

©2020 Matt Trzcinski