使用SAS宏来管理Windows目录中的文件名列表

All*_*owe 7 file-io pipe sas dir sas-macro

我试图修改下面的宏来接受一个宏参数作为dir命令的'location'参数.但是由于嵌套引号问题,我无法正确解析它.使用%str(%')不起作用,也不会出于某种原因引用函数.

当文件路径没有空格(例如C:\ temp\withnospace)时,宏将正常工作,因为不需要中间引号.但是我需要这个宏来处理带空格的文件路径(例如'C:\ temp\with space \').

请帮忙!

%macro get_filenames(location)
   filename pipedir pipe   "dir &location. /b " lrecl=32767;
   data filenames;
     infile pipedir truncover;
     input line $char1000.;
   run;
%mend;

%get_filenames(C:\temp\)              /* works */
%get_filenames('C:\temp\with space')  /* doesnt work */
Run Code Online (Sandbox Code Playgroud)

cmj*_*hns 14

这是实现相同结果而不需要使用PIPE的另一种方法.

%macro get_filenames(location);
filename _dir_ "%bquote(&location.)";
data filenames(keep=memname);
  handle=dopen( '_dir_' );
  if handle > 0 then do;
    count=dnum(handle);
    do i=1 to count;
      memname=dread(handle,i);
      output filenames;
    end;
  end;
  rc=dclose(handle);
run;
filename _dir_ clear;
%mend;

%get_filenames(C:\temp\);           
%get_filenames(C:\temp\with space);
%get_filenames(%bquote(C:\temp\with'singlequote));
Run Code Online (Sandbox Code Playgroud)


Cha*_*ung 6

进行以下几项更改,您的代码将起作用.

%macro get_filenames(location);  %*--(1)--*;
   filename pipedir pipe "dir ""%unquote(&location)"" /b" lrecl=32767; %*--(2)--*;
   data filenames;
     infile pipedir truncover;
     input filename $char1000.;
     put filename=;
   run;
   filename pipedir clear;  %*--(3)--*;
%mend;

%get_filenames(d:\)          
%get_filenames(d:\your dir)  %*--(4)--*;
Run Code Online (Sandbox Code Playgroud)

(1)%macro用分号结束陈述;

(2)用双引号双引号括起宏变量分辨率%unquote;

(3)清除文件句柄; 和

(4)不要单引号输入参数.如有必要,请改为宏报价.

  • 只需更改文件名管道清除; 文件名pipedir清楚; 压制警告. (2认同)