请选择 进入手机版 | 继续访问电脑版
楼主: wanwanle2
4763 12

[问答] 一个外部文件夹中多个EXCEL中多个Sheet导入到SAS [推广有奖]

  • 1关注
  • 22粉丝

博士生

87%

还不是VIP/贵宾

-

威望
0
论坛币
323 个
通用积分
294.6629
学术水平
16 点
热心指数
18 点
信用等级
14 点
经验
286983 点
帖子
307
精华
0
在线时间
369 小时
注册时间
2011-4-13
最后登录
2022-8-1

wanwanle2 在职认证  发表于 2018-1-11 14:59:43 |显示全部楼层 |坛友微信交流群
相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
外部文件夹共有上百个数据集格式统一如下(按月,中间也有漏月的情况,情况较少):201301.xlsx
201302.xlsx
201303.xlsx
......
201712.xlsx

以上的每个数据集中也有统一的格式如下(按天,中间有漏月的情况,情况较多):


20130104
20130107
20130108
20130109
......
20171229
也就是excel是按年月命名的,表中的子页是按照月中的日子命名的(周末和放假期间不在其中)。
如何才能循环导入到SAS里面呢?求大神解答,谢谢。下面提供的三个测试数据集。

循环导入多个EXCEL中多个sheet.zip (492.2 KB)
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:EXCEL sheet xcel exce SHE

流水不朽 发表于 2018-1-11 16:38:40 |显示全部楼层 |坛友微信交流群
/*可以不用理会 文件名是否统一规范*/

/*读取文件夹下面所有文件,并将路径存放在 数据集path_file中*/
filename path_ pipe "dir C:\Users /b /S";
data        path_file;
infile path_;
length path_name $200.;
  input path_name $~;
run;

%macro pub_ip(in_file,out_data);
%if %sysfunc(fileexist(&in_file.)) %then %do;
proc import datafile="&in_file."
                                         out=&out_data.
                                        dbms=excel replace;
                                 SCANTIME=N;
                                 USEDATE=N;
                                 getnames=yes;
                                 sheet="Sheet4";  /*sheet名字自己统一写好*/
                                run;
%end;
%mend pub_ip;
/*调用宏,循环导入excel*/
data        _null_;
        set path_file;
  where index(path_name,'.xlsx');/*筛选指定文件*/
                outname=cats('out_data',put(_n_,z2.));
   call execute('%pub_ip('||path_name||','||outname||')');
run;

使用道具

wanwanle2 在职认证  发表于 2018-1-11 17:07:52 |显示全部楼层 |坛友微信交流群
流水不朽 发表于 2018-1-11 16:38
/*可以不用理会 文件名是否统一规范*/

/*读取文件夹下面所有文件,并将路径存放在 数据集path_file中*/
...
文件路径以及文件名成功读入,但是后面的import过程出错了,因为我的一个excel中有多个sheet,而且不同表中sheet个数不相同,sheet名称也不相同。提供的第一步 input 有错,已经改正。
下面是报错信息
95
96   %macro pub_ip(in_file,out_data);
97   %if %sysfunc(fileexist(&in_file.)) %then %do;
98   proc import datafile="&in_file."
99                                            out=&out_data.
100                                          dbms=excel replace;
101                                   SCANTIME=N;
102                                   USEDATE=N;
103                                   getnames=yes;
104                                   sheet="Sheet4";  /*sheet名字自己统一写好*/
105                                  run;
106  %end;
107  %mend pub_ip;
108  /*调用宏,循环导入excel*/
109  data        _null_;
110          set path_file;
111    where index(path_name,'.xlsx');/*筛选指定文件*/
112                  outname=cats('out_data',put(_n_,z2.));
113     call execute('%pub_ip('||path_name||','||outname||')');
114  run;

NOTE: 从数据集 WORK.PATH_FILE. 读取了 3 个观测
      WHERE INDEX(path_name, '.xlsx');
NOTE: “DATA 语句”所用时间(总处理时间):
      实际时间          0.00 秒
      CPU 时间          0.00 秒


NOTE: CALL EXECUTE 生成了代码行。
1   + proc import datafile="E:\会员持仓结构\SAS分析\循环导入多个EXCEL中多个sheet\201301.xlsx"
                                    out=out_data01
dbms=excel replace;
1   +
                                  SCANTIME=N;
2   +                            USEDATE=N;                                  getnames=yes;
                         sheet="Sheet4";                                   run;

ERROR: 文件“_IMEX_.'Sheet4$'n.DATA”不存在。
ERROR: 导入失败。详细信息,请参阅“SAS 日志”。
NOTE: 由于出错,SAS 系统停止处理该步。
NOTE: “PROCEDURE IMPORT”所用时间(总处理时间):
      实际时间          0.10 秒
      CPU 时间          0.09 秒



3   + proc import datafile="E:\会员持仓结构\SAS分析\循环导入多个EXCEL中多个sheet\201302.xlsx"
                                    out=out_data02
dbms=excel replace;
3   +
                                  SCANTIME=N;
4   +                            USEDATE=N;                                  getnames=yes;
                         sheet="Sheet4";                                   run;

ERROR: 文件“_IMEX_.'Sheet4$'n.DATA”不存在。
ERROR: 导入失败。详细信息,请参阅“SAS 日志”。
NOTE: 由于出错,SAS 系统停止处理该步。
NOTE: “PROCEDURE IMPORT”所用时间(总处理时间):
      实际时间          0.09 秒
      CPU 时间          0.07 秒



5   + proc import datafile="E:\会员持仓结构\SAS分析\循环导入多个EXCEL中多个sheet\201303.xlsx"
                                    out=out_data03
dbms=excel replace;
5   +
                                  SCANTIME=N;
6   +                            USEDATE=N;                                  getnames=yes;
                         sheet="Sheet4";                                   run;

ERROR: 文件“_IMEX_.'Sheet4$'n.DATA”不存在。
ERROR: 导入失败。详细信息,请参阅“SAS 日志”。
NOTE: 由于出错,SAS 系统停止处理该步。
NOTE: “PROCEDURE IMPORT”所用时间(总处理时间):
      实际时间          0.09 秒
      CPU 时间          0.06 秒

使用道具

舍身卫道 发表于 2018-1-11 17:15:02 |显示全部楼层 |坛友微信交流群
楼上的代码无法解决你的问题的,现在问题的难点在于如何知道每个excel中有几个sheet,名称是什么

使用道具

流水不朽 发表于 2018-1-11 17:49:45 |显示全部楼层 |坛友微信交流群
wanwanle2 发表于 2018-1-11 17:07
文件路径以及文件名成功读入,但是后面的import过程出错了,因为我的一个excel中有多个sheet,而且不同表 ...
sheet="Sheet4";     这个内容,要填写 你的excel 里面有数据的sheet 名字,这是导入excel的基本知识?
比如 excel 的 sheet1 有数据,那就填写 sheet="Sheet1"; 能理解不?   

使用道具

舍身卫道 发表于 2018-1-11 17:52:52 |显示全部楼层 |坛友微信交流群
流水不朽 发表于 2018-1-11 17:49
sheet="Sheet4";     这个内容,要填写 你的excel 里面有数据的sheet 名字,这是导入excel的基本知识?
...
楼主 每个excel中都有很多个sheet,你这个方法是要他循环一直改sheet名称吗

使用道具

流水不朽 发表于 2018-1-11 17:59:49 |显示全部楼层 |坛友微信交流群
舍身卫道 发表于 2018-1-11 17:15
楼上的代码无法解决你的问题的,现在问题的难点在于如何知道每个excel中有几个sheet,名称是什么
可以,稍微改一下就可以了。


%macro pub_ip(in_file,out_data);
libname xls excel "&in_file.";
proc sql;
        create table ex_&out_data. as
        select distinct
                   substr(memname,1,index(memname,'$')-1) as memname
          from dictionary.tables
     where upcase(libname)='XLS'
         order by memname;
quit;
libname xls;
%macro del(sheetn,n);
proc import datafile="&in_file."
                                         out=&out_data._&n.
                                        dbms=excel replace;
                                 SCANTIME=N;
                                 USEDATE=N;
                                 getnames=yes;
                                 sheet="&sheetn.";
                                run;
%mend del;
data        _null_;
        set ex_&out_data.;
   call execute('%del('||memname||','||put(_n_,z2.)||')');
run;
%mend pub_ip;

使用道具

流水不朽 发表于 2018-1-11 18:01:19 |显示全部楼层 |坛友微信交流群
流水不朽 发表于 2018-1-11 17:49
sheet="Sheet4";     这个内容,要填写 你的excel 里面有数据的sheet 名字,这是导入excel的基本知识?
...
好像给你发帖子了~~~~~~~~~~~~~~~~~~~

使用道具

流水不朽 发表于 2018-1-11 18:03:13 |显示全部楼层 |坛友微信交流群
舍身卫道 发表于 2018-1-11 17:52
楼主 每个excel中都有很多个sheet,你这个方法是要他循环一直改sheet名称吗
看后面的解决方案

使用道具

流水不朽 发表于 2018-1-11 18:06:05 |显示全部楼层 |坛友微信交流群
wanwanle2 发表于 2018-1-11 17:07
文件路径以及文件名成功读入,但是后面的import过程出错了,因为我的一个excel中有多个sheet,而且不同表 ...
建议多加强基础学些

/*读取文件夹下面所有文件,并将路径存放在 数据集path_file中*/
filename path_ pipe "dir C:\Users /b /S";
data        path_file;
infile path_;
length path_name $200.;
  input path_name $~;
run;

%macro pub_ip(in_file,out_data);
libname xls excel "&in_file.";
proc sql;
        create table ex_&out_data. as
        select distinct
                   substr(memname,1,index(memname,'$')-1) as memname
          from dictionary.tables
     where upcase(libname)='XLS'
         order by memname;
quit;
libname xls;
%macro del(sheetn,n);
proc import datafile="&in_file."
                                         out=&out_data._&n.
                                        dbms=excel replace;
                                 SCANTIME=N;
                                 USEDATE=N;
                                 getnames=yes;
                                 sheet="&sheetn.";
                                run;
%mend del;
data        _null_;
        set ex_&out_data.;
   call execute('%del('||memname||','||put(_n_,z2.)||')');
run;
%mend pub_ip;
/*调用宏,循环导入excel*/
data        _null_;
        set path_file;
  where index(path_name,'.xlsx');/*筛选指定文件*/
                outname=cats('out_data',put(_n_,z2.));
   call execute('%pub_ip('||path_name||','||outname||')');
run;
已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
wanwanle2 + 5 + 2 + 2 + 2 精彩帖子

总评分: 论坛币 + 5  学术水平 + 2  热心指数 + 2  信用等级 + 2   查看全部评分

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-3-29 21:28