在 sas 文件名管道中未解析宏的明显调用
Apparent invocation of macro not resolved in sas filename pipe
我正在使用以下 SAS 代码查找目录 &directory.
下的所有文件及其大小
filename tmp pipe "find &directory. -type f -printf '%p %s\n'";
data all_files;
infile tmp;
length file_path 5. size 8.;
input file_path size;
run;
虽然输出数据tmp
是我想要的,但代码会给我警告。
WARNING: Apparent invocation of macro S not resolved.
我尝试在“%”之前添加一个额外的“%”,即
filename tmp pipe "find &directory. -type f -printf '%%p %%s\n'"
但是没用。
我怎样才能摆脱警告?谢谢。
我也试过%str
和%nrstr
,
filename tmp pipe %str("find &directory. -type f -printf '%p %s\n'");
filename tmp pipe %nrstr("find &directory. -type f -printf '%p %s\n'");
filename tmp pipe %str("find &directory. -type f -printf '%%p %%s\n'");
filename tmp pipe %nrstr("find &directory. -type f -printf '%%p %%s\n'");
filename tmp pipe "find &directory. -type f -printf '%str(%%)p %str(%%)s\n'");
filename tmp pipe "find &directory. -type f -printf '%nrstr(%%)p %nrstr(%%)s\n'");
None 人解决了问题。
这可能对您有所帮助。
%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(path)
宏处理器将在双引号括起来的字符串中查找宏触发器 &
和 %
,但不会在单引号括起来的字符串中查找。您可以使用 quote()
函数将字符串括在单引号中。
%let cmd=find &directory/ -type f -printf '%s %p\n' ;
filename tmp pipe %sysfunc(quote(&cmd,%str(%')));
或者您可以只使用 SAS 代码,避免让宏处理器介入。
您可以使用数据步来调用 FILENAME()
函数,而不是创建 FILENAME 语句。
data _null_;
rc=filename('TMP'
,catx(' ',"find &directory/ -type f -printf",quote('%s %p\n',"'"))
,'PIPE');
put rc= ;
run;
data all_files;
infile tmp truncover;
input size file_path 5. ;
run;
或者您根本无法创建文件引用,而只是使用 INFILE
语句中的 FILEVAR=
选项来传递命令。
data all_files;
length cmd 0;
cmd = catx(' ',"find &directory/ -type f -printf",quote('%s %p\n',"'"));
infile tmp pipe filevar=cmd truncover;
input size file_path 5. ;
run;
注意: 颠倒 printf 字符串中大小和路径的顺序将避免在文件名包含嵌入空格时解析结果时出现问题。
我正在使用以下 SAS 代码查找目录 &directory.
filename tmp pipe "find &directory. -type f -printf '%p %s\n'";
data all_files;
infile tmp;
length file_path 5. size 8.;
input file_path size;
run;
虽然输出数据tmp
是我想要的,但代码会给我警告。
WARNING: Apparent invocation of macro S not resolved.
我尝试在“%”之前添加一个额外的“%”,即
filename tmp pipe "find &directory. -type f -printf '%%p %%s\n'"
但是没用。
我怎样才能摆脱警告?谢谢。
我也试过%str
和%nrstr
,
filename tmp pipe %str("find &directory. -type f -printf '%p %s\n'");
filename tmp pipe %nrstr("find &directory. -type f -printf '%p %s\n'");
filename tmp pipe %str("find &directory. -type f -printf '%%p %%s\n'");
filename tmp pipe %nrstr("find &directory. -type f -printf '%%p %%s\n'");
filename tmp pipe "find &directory. -type f -printf '%str(%%)p %str(%%)s\n'");
filename tmp pipe "find &directory. -type f -printf '%nrstr(%%)p %nrstr(%%)s\n'");
None 人解决了问题。
这可能对您有所帮助。
%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(path)
宏处理器将在双引号括起来的字符串中查找宏触发器 &
和 %
,但不会在单引号括起来的字符串中查找。您可以使用 quote()
函数将字符串括在单引号中。
%let cmd=find &directory/ -type f -printf '%s %p\n' ;
filename tmp pipe %sysfunc(quote(&cmd,%str(%')));
或者您可以只使用 SAS 代码,避免让宏处理器介入。
您可以使用数据步来调用 FILENAME()
函数,而不是创建 FILENAME 语句。
data _null_;
rc=filename('TMP'
,catx(' ',"find &directory/ -type f -printf",quote('%s %p\n',"'"))
,'PIPE');
put rc= ;
run;
data all_files;
infile tmp truncover;
input size file_path 5. ;
run;
或者您根本无法创建文件引用,而只是使用 INFILE
语句中的 FILEVAR=
选项来传递命令。
data all_files;
length cmd 0;
cmd = catx(' ',"find &directory/ -type f -printf",quote('%s %p\n',"'"));
infile tmp pipe filevar=cmd truncover;
input size file_path 5. ;
run;
注意: 颠倒 printf 字符串中大小和路径的顺序将避免在文件名包含嵌入空格时解析结果时出现问题。