File and Directory Manipulation - SAS&R

In this article, we are going to present how to work with files and folders in R and SAS.

First of all, here sharing a good resource about SAS Marcos is related to our topic. That lists a series of useful macros, and the code is very standard and worthy to learn, highly recommended.


For R, I'm willing to recommend the fs R package, which provides a cross-platform, uniform interface file system operations

And then let's begin with our topics.

List of files

Suppose if you want to identify the list of files in a particular directory, in R you can easily choose list.files(). For example list files in a specific directory like the current directory.

list.files(path = "./")

You can also get the files within the subfolders, and just match the .txt files, simple use

list.files(path = "./", pattern = ".txt", recursive = T)

In SAS, limited to my knowledge, just list two approaches. The first approach is to use a filename statement with a pipe device type and dir command in the Windows environment.

filename dirlist pipe 'dir /b E:\Tp\*.txt';

data list;
    length line $200;
    infile dirlist;
    line = strip(_infile_);

filename dirlist clear;

proc print data=list; run;

The second approach is to use the functions dopen and dread with the help from dnum, as the following example.

filename root "E:\Tp";

data list;
    * --  return variables  --;
    length name $ 512;

    * --  directory to inventory  --;
    dirid = dopen('root');

    if dirid <= 0 then
        putlog 'ERR' 'OR: Unable to open directory.';
    nfiles = dnum(dirid);

    do i = 1 to dnum(dirid);

        * --  directory item name  --;
        name = dread(dirid, i);

    rc = dclose(dirid);
    dirid = 0;

The more details for the second approach can be found in these links.

File Exists

Suppose if you want to identify a file called README.md that exists in the current directory, then you can choose the file.exists() function in R, that returns TRUE if the file exists, and FALSE otherwise.


In SAS, fileexist function verifies the existence of the file, and it will return 1 if the file exists, and 0 otherwise.

%let fpath=E:\Tp\test.sas;

%macro fileexists(filepath);
    %if %sysfunc(fileexist(&filepath)) %then
        %put NOTE: The external file &filepath exists.;
    %else  %put ERROR: The external file &filepath does not exist.;
%mend fileexists;


Besides if you want to check if a dataset exists, you can choose the exist function. If for checking a variable exists, varnum is recommended.

File Creates

If you want to create a blank file in R then


In SAS, I'm not sure if the below is the normal way, but it’s definitely simple anyway.

data _null_;
    file "E:\Tp\test.txt";

File Deletes

If you want to delete a specific file in R, then


In SAS, I think we can simply use fdelete function.

filename defile '"E:\Tp\test.txt';
data _null_;

Directory creates

Creating a directory is very similar to a file. The function in R is dir.create() that is very convenient to use. In SAS it can be accomplished using the dlcreatedir option and libname statement with 2 lines of code.

options dlcreatedir;
libname folder 'E:\Tp\dummy';

If you want to create or copy multiple folders or directories, more detailed information can be found in Using SAS® to Create Directories and Duplicate Files.

List specific extension files

You can use the macro as shown below to list all the RTF files which means the extension is .rtf.


/*Parameter Description*/
/*dir         input directory*/
/*ext         file name extension*/
/*out         output dataset*/

%macro ListFilesSpecifyExtension(dir=, ext=, out=);
    %local filrf rc did name i;
    %let rc=%sysfunc(filename(filrf,&dir));
    %let did=%sysfunc(dopen(&filrf));

    /* Use the %IF statement to make sure the directory can be opened. If not, end the macro. */
    %if &did eq 0 %then %do;
        %put Directory &dir cannot be open or does not exist;


    data &out;
        length FileName $200;

        %do i = 1 %to %sysfunc(dnum(&did));
            %let name=%qsysfunc(dread(&did,&i));

            %if %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) %then %do;
                Filename = "&name";

    %let rc=%sysfunc(dclose(&did));
    %let rc=%sysfunc(filename(filrf));
%mend ListFilesSpecifyExtension;


Directory Listings in SAS
Obtaining A List of Files In A Directory Using SAS® Functions
Check if a Specified Object Exists
Using SAS® to Create Directories and Duplicate Files