CreateDirectory.sas

/****************************************************************************/
/*  Program Name  : CreateDirectory                                         */
/*  Purpose       : Create new directory on file system                     */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Author        : Matt Trzcinski                                          */
/*  Last Update   : 2017/11/28                                              */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Input(s)      : None                                                    */
/*  Output(s)     : None                                                    */
/*  Usage         : parent = Parent directory file path. May be given       */
/*                           quoted or unquoted. Should NOT have trailing   */
/*                           slash.                                         */
/*                  child  = Name of new directory to create within the     */
/*                           the parent directory.                          */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Example(s)    : Create the directory C:\new-directory-unquoted          */
/*                                                                          */
/*                      %CreateDirectory(C:\, new-directory);               */
/*                                                                          */
/*                  Create the directory C:\new-directory-quoted            */
/*                                                                          */
/*                      %CreateDirectory('C:\', 'new-directory-quoted');    */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Template      : %CreateDirectory(parent= , child= );                    */
/*                                                                          */
/*--------------------------------------------------------------------------*/
/*  Note(s)       :                                                         */
/*                                                                          */
/*  Development   :                                                         */
/*                                                                          */
/****************************************************************************/

%macro CreateDirectory(parent, child);

  %local
    unquotedParent
    unquotedChild
    parentExists
    unquotedChild
    rc
    ;

  %let unquotedParent = %sysfunc(dequote(&parent.));
  %let unquotedChild  = %sysfunc(dequote(&child.));

  %let parentExists = %sysfunc(fileexist(&unquotedParent.));
  %let childExists  = %sysfunc(fileexist(&unquotedParent.\&unquotedChild.));

  %if &parentExists. and not &childExists. %then %do;
    %let rc = %sysfunc(dcreate(&unquotedChild., &unquotedParent.));
    %put WARNING: [MACRO] Directory "&unquotedParent.\&unquotedChild." was created.;
    %end;

%mend;

Powered by peut-publier

©2020 Matt Trzcinski