首页 > 解决方案 > 在 SAS 中使用嵌套的 Do-Loop 导入 Zip 文件时遇到问题

问题描述

我正在尝试导入 104 个 zip 文件,每个文件包含大约 30 个 excel 文件。我创建了一个 excel 文件,其中包含我要导入的所有 zip 文件的名称 (NYISO_Names)。第一个 do 循环查看所有 zip 文件的名称。接下来,我创建了每个 zip 文件内容的列表。第二个循环遍历内容。

至此,代码导入了第一个 zip 文件(30 个 excel 工作簿);但是,它永远不会移动到剩余的 zip 文件。do-loops的嵌套方式有问题吗?

 %macro test;

%do i=1 %to 6;
    %put ---&i.;

    %let Iteration = &i.;
    data abs;
        set NYISO_Names;
        if _n_ =&i. then call symput("FileName", strip(FileName));
        if _n_ = &i. then call symput("Path", strip(Path));
    run;

    filename inzip zip "G:Desktop\&FileName.";


    /* Read the "members" (files) from the ZIP file */
    data Contents_&i.(keep=memname isFolder sheet);
     length memname $200 isFolder 8;
     fid=dopen("inzip");
     if fid=0 then
      stop;
     memcount=dnum(fid);
     do i=1 to memcount;
      memname=dread(fid,i);
      sheet = substr(memname, 1, length(memname)-4);

      /* check for trailing / in folder name */
      isFolder = (first(reverse(trim(memname)))='/');
      output;
     end;
     rc=dclose(fid);
    run;

    DATA _NULL_; IF 0 THEN SET Contents_1 NOBS=X; CALL SYMPUT('RECCOUNT',X); STOP; RUN;

    %do i = 1 %to &RECCOUNT;
    data abs2;
        set Contents_&Iteration.; 
            if _n_ =&i. then call symput("memname", strip(memname));
            if _n_ =&i. then call symput("sheet", strip(sheet));
        run;

 /* identify a temp folder in the WORK directory */
 filename xl "%sysfunc(getoption(work))/&memname." ;

 /* hat tip: "data _null_" on SAS-L */
 data _null_;
 /* using member syntax here */
 infile inzip(&memname.) 
   lrecl=256 recfm=F length=length eof=eof ;
 file   xl lrecl=256 recfm=N;
 input;
 put _infile_ $varying256. length;
return;
 eof:
stop;
run;

 proc import datafile=xl dbms=csv out=Test&i. replace;

 run;
 %end;
 %end;

 %mend;

 %test();

任何帮助将不胜感激。

标签: importsaszipdo-loops

解决方案


您的嵌套%do循环使用相同的索引变量i

尝试对手头的问题使用具有语义意义的不同变量

%macro test;
  … 
  %do iterationIndex = 1 %to 6;
    … 
    %do referenceIndex = 1 %to &REFCOUNT;
      … 
    %end;
  %end;
  … 
%mend;

%test;

推荐阅读