Get file names from a Unix directory

Getting the file names in a Unix directory can assist in finding out if a specific file exists or generating a macro variable parameter list to complete a variety of functions on each file.

To see how to gererate and use a macro variable click here Create macro variable array from SAS variable

The optional parameter SUBS is used to include the file names of allsub-directories.

This macro uses the following three macro files, mkdirs, var_max_len_char and nobs. This macro uses the mkdirs macro to create a temporary directory to save the results of a Unix find command as input. The macros var_max_len_char and nobs are used to reduce the length all characters variables of the two file generated.

SAS Fifth Dimension Macro-GETFILESDIR

%MACRO GETFILESDIR(DIRECTORY,SUBS=no) ;

Parameter Description
DIRECTORY The Unix directory with the file to be downloaded
SUBS Optional: SUBS=yes To include all sub-directories the Default is SUBS=no

Example

Assumptions:

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

The directory your_unix_directory exists with the following three macro files: mkdirs, var_max_len_char and nobs.

%macro get_files_from_dirs(directory,subs=no)            ;
%put %str(----------->directory=&directory)              ;
%INCLUDE "/your_unix_directory/macros/mkdirs.sas"        ;
%INCLUDE "/your_unix_directory/macros/var_max_len_char.sas";
%INCLUDE "/your_unix_directory/macros/nobs.sas"          ;
%mkdirs(/your_unix_temp_directory)                       ;
x "find &DIRECTORY  -ls  >/your_unix_temp_directory/list.txt";
RUN                                                      ;
%LET CURRENT_YEAR=2008                                   ;
data get_files get_directory                             ;
     format full_name $1000.                             ;
     format directory $256.                              ;
     infile "/your_unix_temp_directory/list.txt" truncover;
     length line $ 256                                   ;
     input line $ char256.                               ;
     format auth $10.                                    ;
     if length(line)>3 then do                           ; 
        auth=scan(line,3,' ')                            ; 
        owner=scan(line,5,' ')                           ; 
        group=scan(line,6,' ')                           ;
        size=int(scan(line,7,' '))                       ; 
        mm=scan(line,8,' ')                              ;
        dd=scan(line,9,' ')                              ;
        time=scan(line,10,' ')                           ;
        if time=:'200' then year=time                    ;
                       else year="&CURRENT_YEAR"         ; 
        if time=:'200' then time=""                      ;
        pos=index(line,'/')                              ;
        if pos>0 then full_name=substr(line,pos)         ;
        format file_type $8.                             ; 
        pos=0                                            ; 
        do ii=1 to length(full_name)                     ;
          if substr(full_name,ii,1)='.' then pos=ii      ;
        end                                              ;
        file_type="blank"                                ;
        if pos>0 then do                                 ;
           file_type=substr(full_name,pos+1)             ;
        end                                              ;
        file_type=lowcase(file_type)                     ;
        comma=","                                        ;
        if full_name ='' then delete                     ;
        pos=0                                            ;
        do ii=1 to length(full_name)                     ;
            if substr(full_name,ii,1)='/' then pos=ii    ;
        end                                              ;
        if pos>1 then do                                 ;
           file_name=substr(full_name,pos+1)             ;
           directory=substr(full_name,1,pos-1)           ;
        end                                              ;
%if &subs^=yes %then %do                                 ;
       if directory="&DIRECTORY"                         ;
%end                                                     ;
       if auth=:'d' then output get_directory            ;
                    else output get_files                ;
     end                                                 ;
     drop line ii pos                                    ;
%put %str(----------->directory=&directory)              ;
RUN                                                      ;
options nosource nonotes                                 ;
%nobs(get_directory)                                     ;
%if &_numobs_>0 %then %var_max_len_char                  ;
options source notes                                     ;
data get_files                                           ;
 set get_files                                           ;
RUN                                                      ;
options nosource nonotes                                 ;
%nobs(get_files)                                         ;
%if &_numobs_>0 %then var_max_len_char                   ;
options source notes                                     ;
proc sort                                                ;
     by directory file_name                              ;
data get_files_from_dir                                  ;
 set get_files                                           ;
RUN                                                      ;
%MEND get_files_from_dir                                 ;