SAS - 仅读取某些文件名

时间:2014-02-19 13:59:40

标签: macros sas filenames

我正在使用以下代码将目录中的文件名写入数据集。目前,它读入该目录中的所有文件名。我在试图弄清楚如何只读取特定的文件名时遇到了麻烦。例如,如何只返回'abc%.txt'等文件?

代码:

%macro get_filenames(location);
filename _dir_ "%bquote(&location.)";
data filenames(keep=fname);
  handle=dopen( '_dir_' );
  if handle > 0 then do;
    count=dnum(handle);
    do i=1 to count;
      fname=dread(handle,i);
      output filenames;
    end;
  end;
  rc=dclose(handle);
run;
filename _dir_ clear;
%mend;


%get_filenames("c:\temp\"); 

1 个答案:

答案 0 :(得分:0)

您可以过滤文件名数据集(避免修改宏)或宏(这样会更有效)。

为清楚起见,我建议修改文件名数据集,除非你有一个特别大的目录..

data new:
set filenames;
if substr(fname,1,3)='abc' and scan(fname,2,'.')='txt';
run;

或者,修改宏:

%macro get_filenames(location);
filename _dir_ "%bquote(&location.)";
data filenames(keep=fname);
  handle=dopen( '_dir_' );
  if handle > 0 then do;
    count=dnum(handle);
    do i=1 to count;
      fname=dread(handle,i);
      /* custom filter */
      if substr(fname,1,3)='abc' and scan(fname,2,'.')='txt' then output filenames;
    end;
  end;
  rc=dclose(handle);
run;
filename _dir_ clear;
%mend;
相关问题