Using SAS Macro to pipe a list of filenames from a Windows directory

前端 未结 8 2199
独厮守ぢ
独厮守ぢ 2020-12-03 12:13

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 nes

8条回答
  •  悲&欢浪女
    2020-12-03 13:09

    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:\.

提交回复
热议问题