Using SAS Macro to pipe a list of filenames from a

2020-01-29 07:46发布

I am trying to amend the macro below to accept a macro parameter as the 'location' argument for a dir command. However I cannot get it to resolve correctly due to the nested quotes issue. Using %str(%') does not work, neither do quoting functions for some reason.

The macro will work fine when the filepath has no spaces (eg C:\temp\withnospace) as the middle quotes aren't needed. However I need this macro to work for filepaths with spaces (eg 'C:\temp\with space\').

Please help!

%macro get_filenames(location)
   filename pipedir pipe   "dir &location. /b " lrecl=32767;
   data filenames;
     infile pipedir truncover;
     input line $char1000.;
   run;
%mend;

%get_filenames(C:\temp\)              /* works */
%get_filenames('C:\temp\with space')  /* doesnt work */

8条回答
淡お忘
2楼-- · 2020-01-29 08:26

it works for me if i call the original macro this way

%get_filenames(""C:\Program Files"")

of course i had to add the semicolon at the end of the %macro statement.

if your directory contains a comma, bad things happen. to fix, use the %str() macro

 %get_filenames(%str(C:\temp\comma, fail)) 

查看更多
我只想做你的唯一
3楼-- · 2020-01-29 08:36

Here's another way of achieving the same result without needing to use a PIPE.

%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(C:\temp\);           
%get_filenames(C:\temp\with space);
%get_filenames(%bquote(C:\temp\with'singlequote));
查看更多
孤傲高冷的网名
4楼-- · 2020-01-29 08:36

Here's one that unscrambles the order of quoting and unquoting:

%let command =%unquote(%str(%')dir "&baseDir.data\*txt"%str(%'));

filename datain pipe &command;

where macro variable basedir can contain spaces and so can the filenames. This combination of %unquote and %str(%') is a frequently occuring macro idiom.

"what if I have single quote in my dir?"

Handling this situation requires a macro quoting function, such as %bquote(); Continuing the example above, this:

%let command =%unquote(%str(%')dir "%bquote(&baseDir.data\*txt)"%str(%'));

should do it.

To avoid infinite iterations of this kind of question, look at Ian Whitlock's paper, A Serious Look at Macro Quoting, which is available here;

There are (many) others, but this is the most widely cited. A little note: anything by Ian Whitlock is probably worthwhile. He writes clearly and his understanding of SAS issues is awesome.

查看更多
做自己的国王
5楼-- · 2020-01-29 08:38

here's a quick macro to pull windows-based directory listings into a sas data set.

%macro DirList(dir);

/* %if &SUBDIR eq %then %let subdir=/s; */        /*** &SUBDIR not defined ****/
filename dirpipe pipe "dir &DIR.\*.* /s /-c";

data dir_list(label="Directory Listing [&DIR.]" drop=re_: _line_ date time);
  format Path
         File   $250.
         ModDT  datetime19.
         Size   16.
         _line_ $32000. ;

  if _N_ = 1 then do;
    re_path=prxparse("/Directory of (.+)/");
    re_subd=prxparse("/(\d\d\/\d\d\/\d\d\d\d)\s+(\d\d:\d\d [A|P]M)\s+\s+(\S.*)/");
    re_file=prxparse("/(\d\d\/\d\d\/\d\d\d\d)\s+(\d\d:\d\d [A|P]M)\s+(\d+)\s+(\S.*)/");
    retain re_: path;
    end;

  infile dirpipe lrecl=32000; input; _line_ = _infile_;

  if lengthn(_line_)=0 then delete;
  else
  if prxmatch(re_path, _line_) then do;
    path=prxposn(re_path, 1, _line_);
    end;
  else
  if prxmatch(re_subd, _line_) then do;
    date=input(prxposn(re_subd, 1, _line_), mmddyy10.);
    time=input(prxposn(re_subd, 2, _line_), time6.);
    ModDT=dhms(date, 0, 0, time);
    File=prxposn(re_subd, 3, _line_);
    size = .D; /*mark subdirectory records*/
    if file not in ('.', '..') then output;
    end;
  else
  if prxmatch(re_file, _line_) then do;
    date=input(prxposn(re_file, 1, _line_), mmddyy10.);
    time=input(prxposn(re_file, 2, _line_), time6.);
    ModDT=dhms(date, 0, 0, time);
    size=input(prxposn(re_file, 3, _line_), 16.);
    file=prxposn(re_file, 4, _line_);
    output;
    end;
run;
filename dirpipe clear;
%mend;

and here's how they get called

%dirlist(c:);
%dirlist(c:\temp);

notice there is no trailing backslash when specifying the base directory. C: not C:\.

查看更多
放我归山
6楼-- · 2020-01-29 08:40

We use this little macro

%macro getdir(dir=,redirect=, switch=);
    options noxwait xsync;
    %if %length(&switch)=0 %then %let switch=b;
    data _null_; 
      xcmd='dir "' || "&dir" || '"' || "/&switch " || ">" || "&redirect";
      put 'generated the following command: ' xcmd=; 
      rc=system(xcmd);
      put 'result code of above command: ' rc=;
    run;
%mend getdir;

Sample Call

%getdir(dir=c:\temp\,redirect=c:\temp\dir.txt) *run;

If you run in batch and don't have the option noxwait xsync the job will hang on the server waiting for an operator response.

查看更多
乱世女痞
7楼-- · 2020-01-29 08:42

Make the following several changes and your code will work.

%macro get_filenames(location);  %*--(1)--*;
   filename pipedir pipe "dir ""%unquote(&location)"" /b" lrecl=32767; %*--(2)--*;
   data filenames;
     infile pipedir truncover;
     input filename $char1000.;
     put filename=;
   run;
   filename pipedir clear;  %*--(3)--*;
%mend;

%get_filenames(d:\)          
%get_filenames(d:\your dir)  %*--(4)--*;

(1) End the %macro statement with a semi-colon;

(2) Surround the macro variable resolution with doubled-up double quotes and %unquote;

(3) Release the file handle by clearing it; and

(4) Don't single quote your input parameter. macro quote instead, if necessary.

查看更多
登录 后发表回答