BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ryanb2
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
filename pipedir pipe %sysfunc(quote( dir "&path" /S  )) lrecl=5000;

View solution in original post

6 REPLIES 6
rogerjdeangelis
Barite | Level 11
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


Ryanb2
Quartz | Level 8
Thanks Roger. I tried the code and for some reason it doesn't work. It's error-free but it results in zero records read. Thanks for the reply.
Reeza
Super User

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);
Ryanb2
Quartz | Level 8
Thanks Reeza. I tried that earlier and the path does resolve but the code doesn't work the same. The log indicates the path is invalid.
Ksharp
Super User
filename pipedir pipe %sysfunc(quote( dir "&path" /S  )) lrecl=5000;
Ryanb2
Quartz | Level 8

Thanks Ksharp!  It worked like a charm.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1706 views
  • 3 likes
  • 4 in conversation