DATA Step, Macro, Functions and more

How do I resolve macro variables as part of pipedir

Accepted Solution Solved
Reply
Contributor
Posts: 26
Accepted Solution

How do I resolve macro variables as part of pipedir

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


Accepted Solutions
Solution
‎04-07-2017 10:25 AM
Super User
Posts: 9,671

Re: How do I resolve macro variables as part of pipedir

filename pipedir pipe %sysfunc(quote( dir "&path" /S  )) lrecl=5000;

View solution in original post


All Replies
Valued Guide
Posts: 505

Re: How do I resolve macro variables as part of pipedir

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


Contributor
Posts: 26

Re: How do I resolve macro variables as part of pipedir

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.
Super User
Posts: 17,784

Re: How do I resolve macro variables as part of pipedir

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);
Contributor
Posts: 26

Re: How do I resolve macro variables as part of pipedir

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.
Solution
‎04-07-2017 10:25 AM
Super User
Posts: 9,671

Re: How do I resolve macro variables as part of pipedir

filename pipedir pipe %sysfunc(quote( dir "&path" /S  )) lrecl=5000;
Contributor
Posts: 26

Re: How do I resolve macro variables as part of pipedir

Thanks Ksharp!  It worked like a charm.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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