【问题标题】:Apparent invocation of macro not resolved in sas filename pipe在 sas 文件名管道中未解析宏的明显调用
【发布时间】:2019-01-24 02:40:46
【问题描述】:

我正在使用以下 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是我想要的,但代码会给我警告。

警告:对宏 S 的明显调用未解决。

我尝试在“%”之前添加一个额外的“%”,即

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'");

他们都没有解决问题。

【问题讨论】:

标签: linux sas pipe sas-macro


【解决方案1】:

宏处理器将在用双引号括起来的字符串内查找宏触发器&%,而不是用单引号括起来的那些。您可以使用quote() 函数将字符串括在单引号中。

%let cmd=find &directory/ -type f -printf '%s %p\n' ;
filename tmp pipe %sysfunc(quote(&cmd,%str(%')));

或者您可以只使用 SAS 代码,避免让宏处理器参与其中。

您可以使用 datastep 来调用 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 字符串中颠倒大小和路径的顺序将避免当文件名包含嵌入空格时解析结果时出现问题。

【讨论】:

    【解决方案2】:

    这可能会对您有所帮助。

    %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)
    

    【讨论】:

      猜你喜欢
      • 2014-09-26
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-07
      • 1970-01-01
      相关资源
      最近更新 更多