Hello,
What you can do is first read the files that are in your folder and sub-folder if applicable and have the names of the files in a dataset. Later you can run a dataset driven program as follows:
Reading the files in your folder: My examples is for Unix, but also should work for Windows after changing to correct path names (Forward slashes to backward)
%macro Files_List(dsn, /* Output Dataset name. <libref>.<dataset name> e.g: Work.Files */
dir, /* Directory name(Unix/Linux only). For windows need to change slashes in below code */
ext /* File extension for search without period. i.e: sas or xlsx or txt or csv */
);
%if %sysfunc(exist(&dsn))=0 %then %do;
/* Create a table */
proc sql;
create table &dsn
( File_Name char(1000) format=$1000.
);
quit;
%end;
%local filrf rc did memcnt name i;
/* Assigns a fileref to the directory and opens the directory */
%let rc=%sysfunc(filename(filrf,&dir));
%let did=%sysfunc(dopen(&filrf));
/* Make sure directory can be open */
%if &did eq 0 %then %do;
%put Directory &dir cannot be open or does not exist;
%return;
%end;
/* Loops through entire directory */
%do i = 1 %to %sysfunc(dnum(&did));
/* Retrieve name of each file */
%let name=%qsysfunc(dread(&did,&i));
/* Checks to see if the extension matches the parameter value */
/* If condition is true print the full name to the log */
%if %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) %then %do;
/* Insert directory path name into table */
proc sql;
insert into &dsn
set File_Name= "&dir/&name";
quit;
%end;
/* If directory name call macro again */
%else %if %qscan(&name,2,.) = %then %do;
%Files_List(&dsn,&dir/%unquote(&name),&ext)
%end;
%end;
/* Closes the directory and clear the fileref */
%let rc=%sysfunc(dclose(&did));
%let rc=%sysfunc(filename(filrf));
%mend Files_List;
/* Invoke the macro, example */
%Files_List(work.test,/user/KiranP,csv);
Now you will have the files (ending CSV ) into a dataset. Using this dataset & Proc Import you can load your csv file to SAS Datasets.
data _null_;
set test;
call execute( cat( "proc import datafile='",trim(File_Name),"' out= ",scan(scan(File_Name,-1,'/'),1,'.') ," dbms=CSV;run;"));
run;
Note: Few things you may have to take into consideration.
1) Are your csv files all Unique, if no then the dataset will be overwritten. Consider changing the dataset names in Proc Import to something Unique even though the file names are not unique.
2) Does proc import really works for you? It works on Guessing rows which you might have to consider, check for data truncation. example: if your columns first 20 rows are having length of 15 or below then proc import will consider a length of 15, but you might have records where length is >15 for later records, in this case your data is truncated to 15. You can use guessingrows=<number> as option for CSV files.
... View more