Get File List and Load File Using FTP in SAS

/**
* Obtain the listing of files in the specified directory using FTP connection.
*/
%macro getFileListing(sourceDir=);

filename dataSrc ftp "" ls user="usename" pass="password" cd="&sourceDir."
host="192.168.245.1";

data Files;
infile dataSrc truncover dsd;
input fileName $255. ;
run;

filename dataSr clear;

%mend getFileListing;

/**
* Load a file using FTP connection. Specify the path on the FTP server and the file name.
*/
%macro loadFile(filePath=, fileName=);

filename iFile ftp "&fileName." user="usename" pass="password" cd="&filePath."
host='192.168.245.1';

data File;
infile iFile;
input contents $3200.;
run;

filename iFile clear;

%mend loadFile;

%getFileListing(sourceDir=/test/user/directory);
%loadFile(filePath=/test/user/directory, fileName=test.txt);

Get Filenames from Directory using SAS Macro

The following SAS macro will get the listing of file and directory names in the specified input directory name and place them into the work.files dataset. The macro works with the paths with special characters (see example usage below).


%macro getFiles(inDirectory);

filename _dir_ "%bquote (&inDirectory.)";

data files (keep = filename);
handle = dopen ( '_dir_' );
if handle > 0 then do;
count = dnum (handle);
do i = 1 to count;
filename = dread (handle, i);
output files;
end;
end;
rc = dclose (handle);
run;

filename _dir_ clear;

%mend;

* %getFiles - populates work.files dataset with the listing of file names in the directory
* specified as a parameter. ;
%getFiles(c:\New Folder);
*%getFilenames(%bquote(c:\Documents and Settings\'directory'));