I found the code below (originally without any of the macro code) that inventories directories and it's lightning quick. I want to turn it into a macro so I can call it when I need it, but when I put the &path in place of the literal path it resolves as &path rather than inserting the path I called in the macro. I tried switching the single and double quotes and it looks like &path resolves to the correct path but now the coding doesn't work (Stderr output: The specified path is invalid.). It works perfectly when I just paste the path into the code. What can I do to turn this into a macro?
Thanks.
%macro get_file_info(path); /* http://support.sas.com/resources/papers/proceedings12/058-2012.pdf */
filename pipedir pipe ' dir "I:\VRBIS\TransaxAchieve-UCD" /S' lrecl=5000;
/* filename pipedir pipe ' dir "&path" /S' lrecl=5000;*/
data indata;
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=indata;
by directory descending date;
run;
data Directory_Summary(drop=i line);
set indata;
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.);
directory_size=input(scan(line,4,' '),comma32.);
end;
file_size=input(scan(line,4,' '),comma32.);
filename=' ';
do i=5 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 get_file_info;
/*%include 'H:\SAS code\Directory_summary.sas';*/
%get_file_info(I:\VRBIS\TransaxAchieve-UCD);
filename pipedir pipe %sysfunc(quote( dir "&path" /S )) lrecl=5000;
I created your directory
Does this help
%macro get_file_info(path);
filename pipedir pipe &path lrecl=5000;
data _null_;
infile pipedir;
input;
putlog _infile_;
run;quit;
filename pipedir clear;
%mend get_file_info;
%get_file_info('dir d:\VRBIS\TransaxAchieve-UCD /S');
Directory of d:\VRBIS\TransaxAchieve-UCD
04/06/2017 07:45 PM <DIR> .
04/06/2017 07:45 PM <DIR> ..
04/06/2017 07:45 PM 131,072 class1.sas7bdat
04/06/2017 07:45 PM 131,072 class2.sas7bdat
2 File(s) 262,144 bytes
Total Files Listed:
2 File(s) 262,144 bytes
2 Dir(s) 248,515,031,040 bytes free
Try reversing your quotes, switch the double with the single and single with double.
Macro variables don't resolve in single quotes and your outer quotes are single.
%macro sample(path);
%put "This is my '&path'";
%mend;
%sample(This is the path);
filename pipedir pipe %sysfunc(quote( dir "&path" /S )) lrecl=5000;
Thanks Ksharp! It worked like a charm.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.