Search code examples
file-iosaspipefilenamessas-macro

using macro variable to read folder content in SAS 9.4


I'm trying to get the list of files in a directory with a SAS macro that uses a macro variable to specify dynamically the folder name. The code I run is the following:

%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;

When I then execute the macro with the argument codice_veicolo being the name of the folder I want to search in, I get the following error:

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

I supposed that for some reason it could not resolve the macro variable, but if I run:

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

I get the proper path and the proper directory, therefore I assume the problem is in the infile statement. The code runs fine without using macro variables.

I am using SAS 9.4 on Windows 8. Any ideas??

Thank you in advance :) Luca


Solution

  • Macro variable references are not expanded inside of single quotes. Try this instead.

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