
%global dest;

%macro chkparam;

 %if (%length(&dest)<=1) %then %do;
   %let dest = %nrbquote(/sddshared/SASWorkspaces/gcdsasautos);
 %end;
%mend chkparam;

%*chkparam;


/** Macro Returns file attributes **/
%macro FileAttribs(fpath);                                                                                                           

  
  %local rc fid fidc;                                                                                                                   
  %local Bytes CreateDT ModifyDT;                                                                                                       
   %let rc=%sysfunc(filename(onefile,&fpath));                                                                                       
   %let fid=%sysfunc(fopen(&onefile));    

   %let infonum = %sysfunc(foptnum(&fid));
   
   data curinfo;
   length fpath $200 infoname $40 infoval $200;
   fpath = strip("&fpath.");
   %do k=1 %to &infonum;
     %let infoname = %sysfunc(foptname(&fid,&k));
     %let infoval = %sysfunc(finfo(&fid,&infoname.));
     %put infoname=&infoname infoval=&infoval;
     infoname = "&infoname.";
     infoval="&infoval";
     output;
   %end;
   run;
   
   %let fidc=%sysfunc(fclose(&fid));                                                                                                    
   %let rc=%sysfunc(filename(onefile));                                                                                                 
%mend FileAttribs;                                                                                                                      
   
 data finfo;
     stop;     
 run;


%macro list_gmacs(dir,ext);                                                                                                                  
  %local filrf rc did memcnt name i;                                                                                                    
                                                                                                                                        
  /* Assigns a fileref to the directory and opens the directory */                                                           
  %let rc=%sysfunc(filename(filrf,&dir));                                                                                               
  %let did=%sysfunc(dopen(&filrf));                                                                                                     
                                                                                                                                        
  /* Make sure directory can be open */                                                                                                 
  %if &did eq 0 %then %do;                                                                                                              
   %put Directory &dir cannot be open or does not exist;                                                                                
   %return;                                                                                                                             
  %end;                                                                                                                                 
   
  
   
   /* Loops through entire directory */                                                                                                 
   %do i = 1 %to %sysfunc(dnum(&did));                                                                                                  
                                                                                                                                        
     /* Retrieve name of each file */                                                                                                   
     %let name=%qsysfunc(dread(&did,&i));                                                                                               
                                                                                                                                        
     /* Checks to see if the extension matches the parameter value */                                                                   
     /* If condition is true print the full name to the log        */                                                                   
      %if %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) %then %do;                                                                       
        %put &dir/&name; 
        
        /** Just pass in the path and file name **/                                                                                                                                     
        %FileAttribs(&dir/&name.);
        
        data finfo;
         set finfo curinfo;
        run;
       
      %end;       
      
     /* If directory name call macro again */                                                                                           
      %else %if %qscan(&name,2,.) = %then %do;                                                                                          
        %list_gmacs(&dir/%unquote(&name),&ext)                                                                                               
      %end;                                                                                                                             
                                                                                                                                        
   %end;                                                                                                                                
                                                                                                                                        
  /* Closes the directory and clear the fileref */                                                                                      
  %let rc=%sysfunc(dclose(&did));                                                                                                       
  %let rc=%sysfunc(filename(filrf));                                                                                                    

proc sort data=finfo out=finfo;
 by fpath infoname;
run;

proc transpose data=finfo out=tfinfo (drop=_NAME_);
 by fpath;
 id infoname;
 var infoval;
run;

title " FILE(S) LIST under  - &dir  ";
proc print data=tfinfo width=min;
run;
                                                                                                                                        
%mend list_gmacs;                                                                                                                            
                                                                                                                                        
/* First parameter is the directory of where your files are stored. */                                                                  
/* Second parameter is the extension you are looking for.           */                                                                  
%*list_gmacs(&dest,sas);



%symdel dest;