
%*global src dest;

* FCOPY function;
* http://support.sas.com/kb/25/074.html - directory listing;

%macro sddcopy(dir,dest,ext);                                                                                                                  


  * Initialize the libname;
  libname test "&dest";
                                                                                                                                        
  %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));           
         %let file_name = %qsysfunc(dread(&did,&i));
         
         %let srcfile =  &dir./&file_name.;
         %let destfile = &dest./&file_name.;
         %put &srcfile. &destfile.;

         filename src "&srcfile.";
         filename dest "&destfile.";

         /* Copy the records of SRC to DEST. */
         data _null_;
            length msg $ 500;
             rc=fcopy('src', 'dest');
             if rc=0 then
                put 'Copied SRC to DEST.';
             else do;
                msg=sysmsg();
                put rc= msg=;
             end;
         run;
      %end;                                                                               
     %end;                                                                                                                               
   %end;                                                                                                                                 
                                                                                                                                        
  /* Closes the directory */                                                                                                            
  %let rc=%sysfunc(dclose(&did));                                                                                                        
                                                                                                                                        
%mend sddcopy;     


%*sddcopy(&src,&dest,sas);

%*symdel src dest;
