该import
过程接受由filename
语句创建的文件引用,并且该filename
语句接受多个文件。因此,您可以执行以下操作:
filename csvs ('file1.csv', 'file2.csv', 'file3.csv', 'file4.csv');
proc import out=work.data
datafile = csvs
DBMS=CSV REPLACE;
GETNAMES=YES;
DATAROW=2;
run;
我不完全确定这将如何与多个 csv 一起工作,每个 csv 都有一个标题,但我似乎记得 SAS 识别每个新文件并根据您的DATAROW=2
声明跳过第一行。
或者,您可以使用宏循环遍历文件夹中的所有文件,导入它们,并将它们附加到您的主集中。这里的答案应该可以帮助您获取文件夹中所有文件的列表。然后您可以执行以下操作:
%macro importcsvs (folder, outputname);
/* I assume this is your macro that takes a folder and returns a dataset */
/* (called filelist) containing the filename in variable: file */
%getallfilesinfolder(&folder, filelist);
/* determine number of files to read */
%let numfiles=0;
data _null_;
set filelist end=last;
if last then call symput('numfiles',put(_n_,best.));
run;
%do i=1 %to &numfiles;
%let curfile=;
data _null_;
/* obs and firstobs =i mean you only read item i in the dataset */
set filelist (obs=&i firstobs=&i);
call symput('curfile',file);
run;
filename csv "&curfile";
proc import out=work.data
datafile = csv
DBMS=CSV REPLACE;
GETNAMES=YES;
DATAROW=2;
run;
data work.&outputname;
set
%if %sysfunc(exist(work.&outputname)) %then %do;
work.&outputname
%end;
work.data;
run;
%end;
%mend;
%importcsvs(/your/folder/with/csvs, newData);