Create Unix directory

Creating a Unix directory can assist SAS programs but this function should be used with care. If the Unix directory exists this macro only lists a message in the SAS log.

To create a Unix directory requries using Unix commands to change the directory to were the new sub-directory is to be created. The Unix command MKDIR is used to create the new sub-directory. This macro will find and create the first node that needs to be created and repeat this process until the entire directory is created.

If the Unix directory needs to be created the macro performs the following. The first step is to find each node of the directory and the number of nodes and list them in the SAS log. The maximum number of nodes is nine and can be changed is necessary. I plan to rewrite this macro to handle any number of nodes but an the time nine was sufficient. This macro requires the first node in the directory to exists and the user has authority to create directories.

SAS Fifth Dimension Macro-MKDIRS

%MACRO MKDIRS(NAME) ;

Parameter Description
NAME The desired Unix directroy

Example

Assumptions: This macro is executed after signing on to Unix and in a Remote session.

%MACRO MKDIRS(NAME)                                          ;
%PUT %STR(----------->NAME EXISTS %sysfunc(fileexist(&NAME)));
%if %sysfunc(fileexist(&NAME))=0 %then %do                   ;
DATA _nulls_                                                 ;
     NUM_NODES=0                                               ;
     LINE="&NAME"                                            ;
     DO II=1 TO LENGTH(LINE)                                 ;
        IF SUBSTR(LINE,II,1)='/' THEN NUM_NODES=SUM(NUM_NODES,1) ;
        IF SUBSTR(LINE,II,1)='/' THEN POS=II                 ;
     END                                                     ;
     WORD1=SCAN(LINE,1,'/')                                  ;
     WORD2=SCAN(LINE,2,'/')                                  ;
     WORD3=SCAN(LINE,3,'/')                                  ;
     WORD4=SCAN(LINE,4,'/')                                  ;
     WORD5=SCAN(LINE,5,'/')                                  ;
     WORD6=SCAN(LINE,6,'/')                                  ;
     WORD7=SCAN(LINE,7,'/')                                  ;
     WORD8=SCAN(LINE,8,'/')                                  ;
     WORD9=SCAN(LINE,9,'/')                                  ;
     CALL SYMPUT('WORD1',SUBSTR(WORD1,1,LENGTH(WORD1)))      ;
     CALL SYMPUT('WORD2',SUBSTR(WORD2,1,LENGTH(WORD2)))      ;
     CALL SYMPUT('WORD3',SUBSTR(WORD3,1,LENGTH(WORD3)))      ;
     CALL SYMPUT('WORD4',SUBSTR(WORD4,1,LENGTH(WORD4)))      ;
     CALL SYMPUT('WORD5',SUBSTR(WORD5,1,LENGTH(WORD5)))      ;
     CALL SYMPUT('WORD6',SUBSTR(WORD6,1,LENGTH(WORD6)))      ;
     CALL SYMPUT('WORD7',SUBSTR(WORD7,1,LENGTH(WORD7)))      ;
     CALL SYMPUT('WORD8',SUBSTR(WORD8,1,LENGTH(WORD8)))      ;
     CALL SYMPUT('WORD9',SUBSTR(WORD9,1,LENGTH(WORD9)))      ;
     CALL SYMPUT('NUM_NODES',COMPRESS(NUM_NODES))            ;
RUN                                                          ;
    %PUT %STR(----------->NUM_NODES=&NUM_NODES)              ;
    %DO II=1 %TO &NUM_NODES                                  ;
        %PUT %STR(----------->WORD&II=&&WORD&II)             ;
    %END                                                     ;
    LET NODE=                                                ;
    %DO II=1 %TO &NUM_NODES                                  ;
        %LET NODE=&NODE/&&WORD&II                            ;
        %if %sysfunc(fileexist(&NODE))=1 %then %do           ;
            %*PUT %STR(----------->NODE=&NODE EXISTS)        ;
        %END                                                 ;
        %else %do                                            ;
            %PUT %STR(----------->NODE=&NODE DOES NO EXIST)  ;
            %IF &II=1 %then %DO                              ;
                %PUT %STR(----------->FIRST NODE MUST EXISTS);
               %goto EXIT_MAC                                ;
            %END                                             ;
        %END                                                 ; 
    %END                                                     ;
    %LET NODE=                                               ; 
    %DO II=1 %TO &NUM_NODES                                  ;
        %LET NODE=&NODE/&&WORD&II                            ;
        %if %sysfunc(fileexist(&NODE))=1 %then %do           ;
            %PUT %STR(----------->NODE=&NODE EXISTS)         ;
        %END                                                 ;
        %else %do                                            ;
            %LET JJ=%EVAL(&II-1)                             ;
            %PUT %STR(----------->JJ=&JJ)                    ;
            %LET PREVIOUS=                                   ;
            %DO SS=1 %TO &JJ                                 ;
                %LET PREVIOUS=&PREVIOUS/&&WORD&SS            ;
            %END                                             ;
            %PUT %STR(----------->PREVIOUS=&PREVIOUS)        ;
            %PUT %STR(----------->NODE=&NODE CREATED)        ;
 x "cd &PREVIOUS"                                            ;
 x "mkdir &NODE"                                             ;
        %END                                                 ;
    %END                                                     ;
%EXIT_MAC:
%END                                                         ;
%ELSE %DO                                                    ;
   %PUT %STR(----------->NEW DIRECTORY EXIST &NAME)          ;
%END                                                         ;
%MEND MKDIRS                                                 ;