我正在尝试修改下面的宏以接受宏参数作为 dir 命令的“位置”参数。但是,由于嵌套引号问题,我无法正确解决它。使用 %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 */
最佳答案
这是另一种无需使用 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));
关于sas - 使用 SAS 宏从 Windows 目录通过管道传输文件名列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1409543/