Published on :
Utility EXTERNAL

dirlist.sas - Macro to list files

This code is also available in: Deutsch Español Français
Awaiting validation
The `dirlist` macro takes two parameters: `path` (directory path) and `outdn` (output dataset name). If `path` is not provided, a default path is used. If `outdn` is empty or `_null_`, file names are printed directly to the SAS© log. Otherwise, a dataset is created with file information. The code uses the 'dir' system command (Windows specific) to get the file list; adaptation (e.g., 'ls' for Linux) would be necessary in a Linux-based SAS© Viya environment. The output dataset is optionally sorted by extension and displayed.
Data Analysis

Type : EXTERNAL


Data comes from an external source: the list of files from an operating system directory, obtained via the 'dir' command (Windows) executed by a 'filename pipe'. This information is then processed to create an internal SAS dataset or displayed in the log.

1 Code Block
MACRO DEFINITION & INITIALIZATION
Explanation :
This block defines the `dirlist` macro with its `path` and `outdn` parameters. It also initializes default values for these parameters if they are not provided. `%superq` is used to check if the parameters are empty, without attempting to resolve them, which avoids errors for uninitialized values.
Copied!
1%macro dirlist(path, outdn);
2 /*set the defualt value of the required params if the value is null*/
3 %IF %superq(path)= %THEN %let path=&pdir.DATA;
4 %IF %superq(outdn)= %THEN %let outdn=_null_;
2 Code Block
FILENAME PIPE
Explanation :
This `filename pipe` statement is crucial. It creates a fileref named `filelist` which, instead of pointing to a physical file, executes an operating system command (`dir "&path" /o:n /b`) and processes its output as an input file. `dir /o:n /b` lists files from the directory specified by `&path` (ordered by name, raw format). It's important to note that 'dir' is a Windows command; for a Linux environment (common for SAS Viya), a command like 'ls' would be needed.
Copied!
1filename filelist pipe %tslit(dir "&path" /o:n /b);
2 
3 Code Block
DATA STEP Data
Explanation :
This block is a DATA step that reads the output of the 'dir' command via the `filelist` fileref. Each line of the output (corresponding to a file name) is read into the `fullname` variable. If the `outdn` parameter is `_null_`, the `fullname` is directly written to the SAS log. Otherwise, the code parses the `fullname` to extract the extension (`extname`) and the file name without extension (`filename`). The `index`, `scan`, `substr`, `length`, `lowcase`, and `call missing` functions are used for this parsing.
Copied!
1DATA &outdn;
2 /*The max length of the name of a PC file is 255*/
3 LENGTH fullname $255 %IF &outdn ne _null_ %THEN filename $255 extname $10 ; ;
4 INFILE filelist truncover dlm="|";
5 INPUT fullname;
6 %IF &outdn=_null_ %THEN put fullname %str(;) ;
7 %ELSE %DO;
8 IF index(fullname, ".") THEN DO;
9 extname=lowcase(scan(fullname, -1, '.'));
10 filename=substr(fullname, 1, LENGTH(fullname)-LENGTH(extname)-1);
11 END;
12 ELSE DO;
13 filename=fullname;
14 call missing(extname);
15 END;
16 %END;
17 RUN;
4 Code Block
PROC SORT
Explanation :
If an output dataset (`outdn`) has been specified (i.e., not `_null_`), this block executes a `PROC SORT`. This procedure sorts the dataset created by the DATA step according to the `extname` variable (file extension), thus organizing files by type.
Copied!
1%IF &outdn ne _null_ %THEN %DO;
2 PROC SORT DATA=&outdn;
3 BY extname;
4 RUN;
5 Code Block
PROC PRINT
Explanation :
Following `PROC SORT` (if `outdn` is not `_null_`), this block uses `PROC PRINT` to display the contents of the `outdn` dataset in the SAS output. A descriptive title is defined for the procedure output, then cleared after execution. This allows viewing the listed files and their details (full name, file name, extension) directly in the SAS output.
Copied!
1 title "The &path contents";
2 PROC PRINT DATA=&outdn;
3 RUN;
4 title;
5 %END;
6 Code Block
CLEANUP & MACRO END
Explanation :
This final block is responsible for cleanup and macro closure. The `filename filelist clear;` statement frees the `filelist` fileref, which is good practice to avoid conflicts or accidental subsequent use. Finally, `%mend dirlist;` marks the end of the `dirlist` macro definition.
Copied!
1 filename filelist clear;
2%mend dirlist;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Name : dirlist.sas Author: Jun Fang June, 2016