At SAS Global Forum I will be presenting a paper on the use of some of the DATA step functions, such as the ones used here. I will add a citation for this blog.

One of the macros presented executes against all the files in a folder, including sub-folders. In this macro, included here, the objective is to import all CSV files, but that is the least important aspect of the macro.

%macro RecursiveImportData(folder=,levelcounter=0);
%* written by Phuong Dinh of Cornerstone Research Inc.;
%local _rc _dsid _i _s_ext _filename _ref;
%let _ref=filrf&levelcounter;
%if %sysfunc(fileexist(&folder)) = 0 %then %do;
%put ERROR: Folder does not exist - &folder;
%return;
%end;
%let _rc = %qsysfunc(filename(_ref, &folder));

%if &_rc=0 %then %do;
%let _dsid = %qsysfunc(dopen(&_ref));
%if &_dsid ne 0 %then %do;
%do _i=1 %to %qsysfunc(dnum(&_dsid));
%let _filename = %qsysfunc(dread(&_dsid, &_i));
%let _s_ext = %scan(&_filename, -1, .);
%if %upcase(&_s_ext)=CSV %then %do;
%put NOTE: Importing &folder\&_filename;
proc import file="&folder\&_filename"
out=temp dbms=csv replace;
getnames=yes;
run;
proc append base=bigdata
data=temp force;
run;
%end;
%else %if &_s_ext= &_filename %then %do;
%put This is a folder: &folder\&_filename;
%recursiveImportData(folder=&folder\&_filename,
levelcounter=%eval(&levelcounter+1))
%end;
%end;
%end;
%let _dsid=%qsysfunc(dclose(&_dsid));
%end;
%let _rc = %qsysfunc(filename(_ref));
%mend recursiveImportData;