全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
1498 3
2014-03-12
大侠
我用下面的程序,可以读一个excel中的所有的sheet
%macro im_1m1excel(RootPath,FileName,Extension);
libname MyExcel Excel "&RootPath.\&Filename..&Extension";

proc sql noprint;
select catt(trim(libname),'.',quote(trim(memname)),'n')
into: namelist separated by ' '
from dictionary.tables
where libname in ('MYEXCEL');
quit;

%put &namelist;
data &FileName;
set &namelist;
run;
%mend im_1m1excel;

%im_1m1excel(d:\,update20140307,xlsx);



但,在文件夹d:\下面有很多的excel,我想也用这个方法批量读d:\所有excel中的所有sheet
而且要用上面这种proc sql 的方式,不用proc improt的方式,因为速度的原因。

请问怎么循环这个宏程序呢?

万分感谢

二维码

扫码加我 拉你入群

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

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

全部回复
2014-3-14 09:58:04
You can use the following to find out all excel files under a directory, then pass it to your macro.

295
296
297  data _null_;
298     array files[1000] $ 256 _temporary_;
299     dnum = 0;
300     trunc = 0;
301     call dir_entries("c:\temp", files, dnum, trunc);
302     if trunc then put 'ERROR: Not enough result array entries. Increase array size.';
303     do i = 1 to dnum;
304        if find(files[i],'.xls') or find(files[i],'.xlsx') then put files[i];
305     end;
306  run;

c:\temp\ALSA_INPUT.xls
c:\temp\book11.xlsx
c:\temp\book221.xlsx
c:\temp\b_swap.xls
c:\temp\check_rateSqrt.xls
c:\temp\class.xls
c:\temp\corr_segerr_remove_industr.xlsx
c:\temp\g.xlsx
c:\temp\label_excel.xls
c:\temp\param2.xls
c:\temp\sdata.xlsx
c:\temp\simudata.xlsx
c:\temp\splited.xlsx
c:\temp\test.xls
c:\temp\test.xlsx
c:\temp\testout.xls
c:\temp\vol.xlsx
c:\temp\YIELDCURVE_spot2.xls
c:\temp\_Book1.xls
c:\temp\_Book2.xls

******************************
options cmplib=sasuser.funcs;
proc fcmp outlib=sasuser.funcs.dir;

function diropen(dir $);
   length dir $ 256 fref $ 8;
   rc = filename(fref, dir);
   if rc = 0 then do;
      did = dopen(fref);
      rc = filename(fref);
   end;
   else do;
      msg = sysmsg();
      put msg '(DIROPEN(' dir= ')';
      did = .;
   end;
   return(did);
endsub;

subroutine dirclose(did);
   outargs did;
   rc = dclose(did);
   did = .;
endsub;

subroutine dir_entries(dir $, files[*] $, n, trunc);
   outargs files, n, trunc;
   length dir entry $ 256;

   if trunc then return;

   did = diropen(dir);
   if did <= 0 then return;

   dnum = dnum(did);
   do i = 1 to dnum;
      entry = dread(did, i);
      /* If this entry is a file, then add to array */
      /* Else entry is a directory, recurse. */
      fid = mopen(did, entry);
      entry = trim(dir) || '\' || entry;
      if fid > 0 then do;
         rc = fclose(fid);
         if n < dim(files) then do;
            trunc = 0;
            n = n + 1;
            files[n] = entry;
         end;
         else do;
            trunc = 1;
            call dirclose(did);
            return;
         end;
      end;
      else
         call dir_entries(entry, files, n, trunc);
   end;

   call dirclose(did);
endsub;
run;
二维码

扫码加我 拉你入群

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

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

2014-3-15 00:35:54
Use call execute

%macro read_one(RootPath,FileName,Extension);
libname MyExcel Excel "&RootPath.\&Filename..&Extension";

proc sql noprint;
select catt(trim(libname),'.',quote(trim(memname)),'n')
into: namelist separated by ' '
from dictionary.tables
where libname in ('MYEXCEL');
quit;

%put &namelist;
data &FileName;
set &namelist;
run;
%mend;

%macro readin (dir);
  filename myfiles pipe "dir/b %bquote(&dir.\*.xls*)";
  data af;
    infile myfiles;
        input;
        filename=scan(_infile_,1);
        ext=scan(_infile_,2);
        call execute ('%nrstr(%read_one(%bquote(&dir),'||strip(filename)||','||strip(ext)||');');
        run;
%mend;
二维码

扫码加我 拉你入群

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

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

2014-3-27 18:35:05
非常感谢各位的回复。谢谢
二维码

扫码加我 拉你入群

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

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

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群