我正在使用以下 SAS 代码查找目录 &directory.
下的所有文件及其大小。
filename tmp pipe "find &directory. -type f -printf '%p %s\n'";
data all_files;
infile tmp;
length file_path $255. 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'");
他们都没有解决问题。
最佳答案
宏处理器将在双引号括起来的字符串中查找宏触发器 &
和 %
,但不会在单引号括起来的字符串中查找。您可以使用 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 $255. ;
run;
或者您根本无法创建文件引用,而只是在 INFILE
语句中使用 FILEVAR=
选项来传递命令。
data all_files;
length cmd $200;
cmd = catx(' ',"find &directory/ -type f -printf",quote('%s %p\n',"'"));
infile tmp pipe filevar=cmd truncover;
input size file_path $255. ;
run;
注意:颠倒 printf 字符串中大小和路径的顺序将避免在文件名包含嵌入空格时解析结果时出现问题。
关于linux - 在 sas 文件名管道中未解析宏的明显调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54338636/