如何编写sas代码以读取不同文件夹中的文件?

如何编写sas代码以读取不同文件夹中的文件?,sas,sas-macro,Sas,Sas Macro,我想知道是否可以写一个宏来读取文件 在不同的文件夹中 例如,我有100个文件夹,每个文件夹中, 它有另一个文件夹,在子文件夹中,它可能包含一个我需要的txt文件 想与SAS一起阅读。可以创建新文件夹以包含新文件 Folder1 folder2文件 A1 aa1文件1 A2 aa2(无文件) A100 aa100文件100 Folder2位于folder1中,文件存储在Folder2中 谢谢 陈旭以下宏可能会起作用 %macro procesFilesInRoot(rootFolder,fi

我想知道是否可以写一个宏来读取文件 在不同的文件夹中

例如,我有100个文件夹,每个文件夹中, 它有另一个文件夹,在子文件夹中,它可能包含一个我需要的txt文件 想与SAS一起阅读。可以创建新文件夹以包含新文件

Folder1 folder2文件

A1 aa1文件1

A2 aa2(无文件)

A100 aa100文件100

Folder2位于folder1中,文件存储在Folder2中

谢谢


陈旭

以下宏可能会起作用

  %macro procesFilesInRoot(rootFolder,fileExt,procesMacro);  
        %put "dir ""%unquote(&rootFolder)"" /s /b";

      filename pipeTree pipe "dir ""%unquote(&rootFolder)"" /s /b" lrecl=32767; 
      data fullNames;
          infile pipeTree truncover;
          input fullName $char1000.;
              ext = upcase(scan(fullName,countw(fullName, '.'), '.'));
          if ext = "%upcase(&fileExt)";
      run;
      filename pipeTree clear; 

        title 'files found';
        proc print;
        run; 

      proc sql noprint;
          select count(*) into :nrFiles from fullNames;
          %let nrFiles = &nrFiles; /** to strip the leading blanks **/
              %if &nrFiles %then %do;
              select fullName into :fullName1-:fullName&nrfiles from fullNames;
              %end;
      quit;

      %do fileNr = 1 %to &nrFiles.;
          %&procesMacro(&&fullName&fileNr);
      %end;
  %mend;
  %macro import1file(fullName);
      %let fileName = %scan(&fullName,%sysfunc(countw(&fullName, '\')), '\');
      %let fileExt = %scan(&fileName,%sysfunc(countw(&fullName, '.')), '.');
      %let dataName = %substr(&fileName,1, %length(&fileName)-%length(&fileExt)-1);

      proc import datafile = "&fullName" out=&dataName;
      run;
  %mend;
  %procesFilesInRoot(D:\Horsten,txt,import1file);
在使用它之前,您需要编写一个宏来处理单个输入文件

  %macro procesFilesInRoot(rootFolder,fileExt,procesMacro);  
        %put "dir ""%unquote(&rootFolder)"" /s /b";

      filename pipeTree pipe "dir ""%unquote(&rootFolder)"" /s /b" lrecl=32767; 
      data fullNames;
          infile pipeTree truncover;
          input fullName $char1000.;
              ext = upcase(scan(fullName,countw(fullName, '.'), '.'));
          if ext = "%upcase(&fileExt)";
      run;
      filename pipeTree clear; 

        title 'files found';
        proc print;
        run; 

      proc sql noprint;
          select count(*) into :nrFiles from fullNames;
          %let nrFiles = &nrFiles; /** to strip the leading blanks **/
              %if &nrFiles %then %do;
              select fullName into :fullName1-:fullName&nrfiles from fullNames;
              %end;
      quit;

      %do fileNr = 1 %to &nrFiles.;
          %&procesMacro(&&fullName&fileNr);
      %end;
  %mend;
  %macro import1file(fullName);
      %let fileName = %scan(&fullName,%sysfunc(countw(&fullName, '\')), '\');
      %let fileExt = %scan(&fileName,%sysfunc(countw(&fullName, '.')), '.');
      %let dataName = %substr(&fileName,1, %length(&fileName)-%length(&fileExt)-1);

      proc import datafile = "&fullName" out=&dataName;
      run;
  %mend;
  %procesFilesInRoot(D:\Horsten,txt,import1file);
然后您可以按如下方式使用它

  %macro procesFilesInRoot(rootFolder,fileExt,procesMacro);  
        %put "dir ""%unquote(&rootFolder)"" /s /b";

      filename pipeTree pipe "dir ""%unquote(&rootFolder)"" /s /b" lrecl=32767; 
      data fullNames;
          infile pipeTree truncover;
          input fullName $char1000.;
              ext = upcase(scan(fullName,countw(fullName, '.'), '.'));
          if ext = "%upcase(&fileExt)";
      run;
      filename pipeTree clear; 

        title 'files found';
        proc print;
        run; 

      proc sql noprint;
          select count(*) into :nrFiles from fullNames;
          %let nrFiles = &nrFiles; /** to strip the leading blanks **/
              %if &nrFiles %then %do;
              select fullName into :fullName1-:fullName&nrfiles from fullNames;
              %end;
      quit;

      %do fileNr = 1 %to &nrFiles.;
          %&procesMacro(&&fullName&fileNr);
      %end;
  %mend;
  %macro import1file(fullName);
      %let fileName = %scan(&fullName,%sysfunc(countw(&fullName, '\')), '\');
      %let fileExt = %scan(&fileName,%sysfunc(countw(&fullName, '.')), '.');
      %let dataName = %substr(&fileName,1, %length(&fileName)-%length(&fileExt)-1);

      proc import datafile = "&fullName" out=&dataName;
      run;
  %mend;
  %procesFilesInRoot(D:\Horsten,txt,import1file);

1.是否所有文件都具有完全相同的结构2。这是在Windows还是Unix 3中。您能否通过计算机上的SAS运行系统命令,即X命令或管道?您需要给出一个更具体的示例。“可能包含”是随机的还是模式化的?您想将所有这些数据读入一个数据集,还是想将它们读入100个(或更多)数据集?如果您想探索其他人如何处理类似问题,您可能希望看看@SRSwift-奇妙的东西,感谢您帮助这个问题波斯尔和其他人。