*含有多个工作簿的EXCEL文件转换成多个SAS数据集;
libname rawexcel "C:\Users\Administrator\Desktop\TEST1.XLSX" access=readonly; *请修改:原始EXCEL文件路径以及文件名和后缀;
LIBNAME BARN "C:\Users\Administrator\Desktop\SASDATASET"; *请修改:存放SAS数据集的位置;
%MACRO EXCELTOSAS();
PROC SQL NOPRINT;
SELECT COUNT(DISTINCT(MEMNAME)) INTO :NUM
FROM SASHELP.VTABLE
WHERE LIBNAME="RAWEXCEL";
SELECT DISTINCT(MEMNAME) INTO :S1-:S%TRIM(%LEFT(&NUM.))
FROM SASHELP.VTABLE
WHERE LIBNAME="RAWEXCEL";
SELECT DISTINCT(COMPRESS(MEMNAME,"',$")) INTO :V1-:V%TRIM(%LEFT(&NUM.))
FROM SASHELP.VTABLE
WHERE LIBNAME="RAWEXCEL";
QUIT;
%DO I=1 %TO &NUM.;
%put &&v&i;
%put &&s&i;
PROC IMPORT DATAFILE="C:\Users\Administrator\Desktop\TEST1.XLSX"
DBMS=XLSX
OUT=BARN.&&v&i
REPLACE;
SHEET="&&v&I.";
GETNAMES=YES;
QUIT;
%END;
%MEND;
%EXCELTOSAS();
值得讲一讲的是SASHELP.VTABLE这个数据集,在运行了libname rawexcel...这个语句后,SAS会创建一个名叫rawexcel的逻辑库,然后SASHELP.VTABLE这个数据集就会新增一条该库的观测,储存了这个库的库名、库里头数据集的名称、数据集的个数等等。所以可以利用VTABLE里头的信息写一个循环自动导出多个sheets的内容到不同的SAS数据集;