简体   繁体   中英

SAS, handle Macro output

I took a SAS macro from SAS website, in order to list all the file in a folder. Thit is the full reference: http://support.sas.com/kb/25/074.html .

And that's the code:

%macro drive(dir,ext);                                                                                                                        
  %let filrf=mydir;                                                                                                                       
  /* Assigns the fileref of mydir to the directory and opens the directory */                                                                    
  %let rc=%sysfunc(filename(filrf,&dir));                                                                                                
  %let did=%sysfunc(dopen(&filrf));                                                                                                       
  /* Returns the number of members in the directory */                                                                   
  %let memcnt=%sysfunc(dnum(&did));                                                                                                     
   /* Loops through entire directory */                                                                                                  
   %do i = 1 %to &memcnt;               
     /* Returns the extension from each file */                                                                                                                                    
     %let name=%qscan(%qsysfunc(dread(&did,&i)),-1,.);                                                                                                 
     /* Checks to see if file contains an extension */                                                                                     
     %if %qupcase(%qsysfunc(dread(&did,&i))) ne %qupcase(&name) %then %do;                                                                     
     /* Checks to see if the extension matches the parameter value */                                                                      
     /* If condition is true prints the full name to the log       */                                                                      
      %if (%superq(ext) ne and %qupcase(&name) = %qupcase(&ext)) or                                                                       
         (%superq(ext) = and %superq(name) ne) %then %do;                                                                                     
         %put %qsysfunc(dread(&did,&i));                  
      %end;                                                                               
     %end;                                                                                                                               
   %end;                                                                                                                                 
  /* Closes the directory */                                                                                                            
  %let rc=%sysfunc(dclose(&did));                                                                                                                                 
%mend drive;                                                                                                                            

/* First parameter is the directory of where your files are stored. */                                                                
/* Second parameter is the extension you are looking for.           */                                                                
/* Leave 2nd paramater blank if you want a list of all the files.   */                                                                
%drive(c:\,sas)

This macro (obviously) works fine, problem is that returns the results on the log. I need to put those results into a SAS dataset in order to schedule other operations. How can I do it?

Thanks in advance.

First off, if you can make a pipe, you can do:

filename dirlist pipe "dir /b c:\*.sas";

data myfiles;
infile dirlist lrecl=512 truncover;
input 
@1 fullname $512.;
filename = scan(fullname,-1,'\');
run;

If you do need to use that macro due to system restrictions, you can't just take the output from it directly as it doesn't do anything other than print to the log. You'll need to do one of two things:

  • Use PROC PRINTTO to redirect the log to a file, which you can then parse
  • Change the line of code that actually does something. %put %qsysfunc(dread(&did,&i)); If you change this to, say,

    filename = dread(&did,&i); output;

then you call the macro from inside a data step and you can use the results. Realistically you might be better off just running all of that in a datastep and not bothering with the macro - it's more complicated than it needs to be in order to be macro-only; in a datastep that's shorter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM