Pergunta

I'm using the following code write the filenames from a directory into a dataset. Currently it reads in all the filenames in that directory. I'm having trouble trying to figure out how to read in only specific filenames. For example, how can I return only files like 'abc%.txt'?

Code:

%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\"); 
Foi útil?

Solução

You can filter either the filenames dataset (avoids modifying the macro) or in the macro (which would be more efficient).

For clarity, I'd recommend modifying the filenames dataset, unless you have a particularly large directory..

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

alternatively, to modify the macro:

%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;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top