Hi, I have to read a folder in unix containg multiple csv files with the same structure and create a dataset with all the variables in these files. Since the file names change, I can't use these finlenames in infile statement.
Is it possible to use any macro to perform this task?
Thank you!
Use the FILENAME= option of the INFILE statement.
data want;
length fname filename $200;
infile "/path/*.csv" filename=fname /* other options */;
input
.....
;
filename = fname;
run;
The two variables are necessary because the one used in the option will automatically be excluded from any output dataset.
Next code is designed to generate a temporary program to do what you want.
It contains few steps:
1) create a list of files to read into a sas dataset.
2) a simple macro to read one file and create a TEMP data set.
3) a dad step to generate the required code as temporary program.
it is recommended to put it to log first for checking and debug.
4) %include the temporary program to execute it
%macro dirlist(path,suffix);
data dirlist(keep=path fname);
retain path "&path";
rc=filename ("fdir","&path");
did=dopen("fdir");
if did>0 then do;
num=dnum(did);
/* call symput('n_files',put(num,3.)); <<< not used */
do i = 1 to num;
fname=dread(did,i);
if lowcase(scan(fname,-1,'.')) = lowcase("&suffix")
then output;
end;
end;
run;
%mend;
%dirlist(<path>,csv);
%macro code;
data temp;
... enter here your INPUT satement code to read a record ...
run;
%mend;
%let want = <output dataset name >;
filename prog "<path>/temp_prog.sas";
data _null_;
set dirlist;
file prog; /* for easy debug replace with put log */
if _n_ =1 then do;
cmd = "proc delete data=&want; run;";
put cmd;
end;
cmd = "filename finp '" ||strip(path)||'/'||strip(fname)||".csv)';";
put cmd;
cmd = "%code;";
put cmd;
cmd = "proc append base=&want data=temp; run;";
put cmd;
cmd = 'filename finp clear;';
put cmd;
run;
/** if seems ok then run next line **/
%include prog;
code was not tested. if any issue please post the log using FILE LOG instead FILE PROG.
Use the FILENAME= option of the INFILE statement.
data want;
length fname filename $200;
infile "/path/*.csv" filename=fname /* other options */;
input
.....
;
filename = fname;
run;
The two variables are necessary because the one used in the option will automatically be excluded from any output dataset.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.