Macros 如何使用宏在SAS中读取多个excel文件?
我想通过导入多个excel文件在SAS中创建一个数据集。excel文件在中具有相同的变量并保存在同一目录中,所有文件都具有一致的名称: excel_20150101 excel_20150201Macros 如何使用宏在SAS中读取多个excel文件?,macros,sas,Macros,Sas,我想通过导入多个excel文件在SAS中创建一个数据集。excel文件在中具有相同的变量并保存在同一目录中,所有文件都具有一致的名称: excel_20150101 excel_20150201 我将如何在SAS中编写此文件 此宏将循环遍历目录中的所有Excel文件,并将导入名为DS1、DS2、DS3…的数据集中的内容…。DS400(如果有400个Excel文件)。请确保在特定目录中仅保留Excel(.xlsx)文件 options merror mlogic mprint symbolgen
我将如何在SAS中编写此文件 此宏将循环遍历目录中的所有Excel文件,并将导入名为DS1、DS2、DS3…的数据集中的内容…。DS400(如果有400个Excel文件)。请确保在特定目录中仅保留Excel(.xlsx)文件
options merror mlogic mprint symbolgen spool;
%macro drive(dir,ext);
%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));
/* Loops through entire directory */
%do i = 1 %to %sysfunc(dnum(&did));
/* Retrieve name and import each Excel file */
%let name=%qsysfunc(dread(&did,&i));
proc import
out=DS&i
datafile= "Y:\Excel\&name"
dbms=XLSX replace;
getnames=yes;
run;
%end;
/* Closes the directory and clear the fileref */
%let rc=%sysfunc(dclose(&did));
%let rc=%sysfunc(filename(filrf));
%mend drive;
/* First parameter is the directory of where your files are stored. */
/* Second parameter is the extension you are looking for. */
%drive(Y:\Excel,xlsx);
下面是如何创建包含所有文件名及其路径的数据集
%let windowpath=/data/exports/Analytics/Users/psamson/Travel_project/PCA/;
data file; /*THIS PREPARE A TABLE OF TARGET FILE TO IMPORT*/
length file $200 name $28;
rc=filename("myfile","&windowpath");
DirId=dopen("myfile");
memcount=dnum(DirId);
do i = 1 to memcount;
fdir = "&windowpath./" || strip(dread(DirId,i));
rc2=filename("fdir",fdir);
fDirId=fopen("fdir");
if fDirId > 0 then do;
file = strip(dread(DirId,i));
name = substr(strip(dread(DirId,i)),1,25);
if index(file,'.xlsx') then output; /*THIS ENSURE WE ONLY IMPORT CSV file */
end;
rc2 = fclose(fDirId);
rc2= filename("fdir", "");
end;
rc = dclose(DirId);
rc = filename("myfile");
keep file name ; /*KEEP THE 2 VARIABLE TO VALIDATE AND USE FOR IMPORT*/
run;
从这个列表中,通过文件表循环调用您的导入,就像上面的宏一样 所以这会很有趣。从Excel导入数据时,类型不受控制,您几乎没有控制权。因此,我99%肯定您会遇到合并数据的问题,因为类型不会对齐。我建议您将所有文件转换为CSV(通过批处理脚本),然后一次导入所有CSV 下面是我编写的一个宏,它导入了所有:
%*Creates a list of all files in the DIR directory with the specified extension (EXT);
%macro list_files(dir,ext);
%local filrf rc did memcnt name i;
%let rc=%sysfunc(filename(filrf,&dir));
%let did=%sysfunc(dopen(&filrf));
%if &did eq 0 %then
%do;
%put Directory &dir cannot be open or does not exist;
%return;
%end;
%do i = 1 %to %sysfunc(dnum(&did));
%let name=%qsysfunc(dread(&did,&i));
%if %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) %then
%do;
%put &dir\&name;
%let file_name = %qscan(&name,1,.);
%put &file_name;
data _tmp;
length dir $512 name $100;
dir=symget("dir");
name=symget("name");
path = catx('\',dir,name);
the_name = substr(name,1,find(name,'.')-1);
run;
proc append base=list data=_tmp force;
run;
quit;
proc sql;
drop table _tmp;
quit;
%end;
%else %if %qscan(&name,2,.) = %then
%do;
%list_files(&dir\&name,&ext)
%end;
%end;
%let rc=%sysfunc(dclose(&did));
%let rc=%sysfunc(filename(filrf));
%mend list_files;
%*Macro to import a single file, using the path, filename and an output dataset name must be specified;
%macro import_file(path, file_name, dataset_name );
proc import
datafile="&path.\&file_name."
dbms=xlsx
out=&dataset_name replace;
run;
%mend;
*Create the list of files, in this case all XLSX files;
%list_files(c:\_localData\temp, xlsx);
%*Call macro once for each entry in the list table created from the %list_files() macro;
data _null_;
set list;
string = catt('%import_file(', dir, ', ', name,', ', catt('test', put(_n_, z2.)), ');');
call execute (string);
run;
但是,我建议将所有文件转换为CSV,然后导入这些文件。
然后将所有CSV一次导入到单个文件中:
它们位于多个不同的excel工作簿中,而不是工作表中。我想循环浏览每个文件,然后创建一个数据集。所有工作表都以相同的文本开头,只是结尾的日期不同。然后您可以将Excel名称作为参数传递,并省略sheet=语句。谢谢,如果使用上述两个文件名,这将如何编写?这里是SAS新手。谢谢,有没有一种方法可以写这个来循环文件,而不是写出宏的最后一部分?有400多个文件。使用日期部分写入do循环?注意,根据您的操作系统,您可能需要在路径中切换/到\以上内容是为UnixTransks编写的,在SAS中确实需要,但对于unix也很熟悉。与每次创建400多个数据集不同,我们是否能够在首次导入后使用新的每日文件每次更新数据集?噢,SAS在windows、unix或linux上运行。文件的路径/路径必须基于您的操作系统!windows或Unix上的SAS也是如此!它有时会帮助您了解SAS行为