使用宏变量读取SAS 9.4中的文件夹内容

时间:2017-03-08 12:19:38

标签: file-io sas pipe filenames sas-macro

我尝试使用SAS宏获取目录中的文件列表,该宏使用宏变量动态指定文件夹名称。我运行的代码如下:

%macro veicolo(codice_veicolo);

filename pipedir pipe ' dir "some_path\&codice_veicolo" /S' lrecl=5000; 

data &codice_veicolo;
 infile pipedir truncover;
 input line $char1000.;
 length directory $1000;
 retain directory;
 if line =' ' or
 index(upcase(line),'<DIR>') or
 left(upcase(line))=:'VOLUME' then
 delete;
 if left(upcase(line))=:'DIRECTORY OF' then
 directory=left(substr(line,index(upcase(line),'DIRECTORY OF')+12));
 if left(upcase(line))=:'DIRECTORY OF' then
 delete;
 if input(substr(line,1,10),?? mmddyy10.) = . then
 substr(line,1,10)='12/31/2999';
 date=input(substr(line,1,10),?? mmddyy10.);
 format date mmddyy10.;
run;

proc sort data=&codice_veicolo;
 by directory descending date;
run;

data folder_&codice_veicolo(drop=i line);
 set &codice_veicolo;
 by directory;
 length filename $75;
 retain number_of_files_in_directory directory_size;
 if first.directory then
 do;
 number_of_files_in_directory=input(scan(line,2,' '),32.);
 call symput(nfiles,number_of_files_in_directory);
 directory_size=input(scan(line,4,' '),comma32.);
 end;
 file_size=input(scan(line,3,' '),comma32.);
 filename=' ';
 do i=4 to 100;
 filename=trim(left(filename))||' '||scan(line,i,' ');
 if scan(line,i,' ')=' ' then
 leave;
 end;
 if index(upcase(line),'FILE(S)') then
 delete;
 if date ge '30DEC2999'd then
 delete;


run;  


%mend;

当我执行宏,参数codice_veicolo是我想要搜索的文件夹的名称时,我收到以下错误:

Output Err std:
The system cannot find the path specified.
NOTE: 20 records were read from the infile PIPEDIR.
      The minimum record length was 0.
      The maximum record length was 90.
NOTE: The data set WORK.JL6AME1A6FK000442 has 2 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.05 seconds
      cpu time            0.01 seconds

我认为由于某种原因它无法解析宏变量,但如果我运行:

%let pgmpath = %sysfunc(pathname(pipedir));
%put &pgmpath;

我得到了正确的路径和正确的目录,因此我假设问题出现在infile语句中。代码运行正常而不使用宏变量。

我在Windows 8上使用SAS 9.4。任何想法??

提前谢谢你:) 卢卡

1 个答案:

答案 0 :(得分:1)

宏变量引用不会在单引号内扩展。 试试这个。

filename pipedir pipe %sysfunc(quote(dir /s "some_path\&codice_veicolo")) lrecl=5000;