使用SAS宏从Windows目录中管理文件名列表 [英] Using SAS Macro to pipe a list of filenames from a Windows directory
问题描述
我正在修改下面的宏接受一个宏参数作为dir命令的'location'参数。但是,由于嵌套引号问题,我无法正确解析它。使用%str(%')不起作用,由于某种原因,引用函数也不起作用。
I am trying to amend the macro below to accept a macro parameter as the 'location' argument for a dir command. However I cannot get it to resolve correctly due to the nested quotes issue. Using %str(%') does not work, neither do quoting functions for some reason.
当文件路径具有无空格(例如C:\temp\withnospace),因为不需要中间引号。但是,我需要这个宏用于具有空格的文件路径(例如'C:\temp\with space\')。
The macro will work fine when the filepath has no spaces (eg C:\temp\withnospace) as the middle quotes aren't needed. However I need this macro to work for filepaths with spaces (eg '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。
Here's another way of achieving the same result without needing to use a 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宏从Windows目录中管理文件名列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!