<--- %%NOBANNER%% --> filescan.sas
Make your own free website on Tripod.com
 BackForward
%*--------------------------------------------------------------------------------*
 | SOURCE:   filescan.sas                                                         |
 |                                                                                |
 | VERSION:  RDS v3.0                                                             |
 |                                                                                |
 | PURPOSE:  This macro scans an input file for occurences of all variable names  |
 |           in a given SAS dataset.  It is designed to scan a SAS program to     |
 |           determine if there are any variables in a SAS dataset that are not   |
 |           used by that program.  If so, the macro prints a report.  If not,    |
 |           a note is put in the log.                                            |
 |                                                                                |
 | SYNTAX:   %filescan (data = _SAS_dataset_name_,                      |
 |                      file = _SAS_program_file_to_scan_)              |
 |                                                                                |
 *--------------------------------------------------------------------------------* ;
%macro filescan (indata=, file=) /des='Scans a file for variable names' ;
   %let notes = %sysfunc(getoption(notes,keyword)) ;
   options nonotes ;
   %let starttime = %sysfunc(datetime()) ;

   proc contents data=&indata noprint out=macro.contents (keep = name) ;
   run ;

   proc transpose data=macro.contents out=macro.tranny (keep = col1-col%obscnt(macro.contents)) ;
      var name ;
   run ;

   data macro.view1 (keep = line n name) /view=macro.view1 ;
      if _n_ = 1 then set macro.tranny ;
      infile &file pad ;

      label n = 'Line number in source code'
            name = 'Variable name'
            line = 'Line of source code' ;

      input @1 line $200. ;
      n = _n_ ;

      %do i = 1 %to %obscnt(macro.contents) ;
         if indexw(line, col&i) ne 0 then do ;
            name = col&i ;
            output ;
         end ;
      %end ;
   run ;

   proc sort data=macro.view1 out=macro.sorted1 ;
      by name ;
   run ;

   data macro.missed (keep = name data file) ;
      merge macro.contents (in = in1)
            macro.sorted1  (in = in2) ;
      by name ;
      if in1 and not in2 ;
      label data = 'Input Dataset Name'
            file = 'SAS Source Code File Scanned' ;
      retain data "&indata" file "&file" ;
   run ;

   %if %obscnt(macro.missed) = 0 %then %do ;
     options ¬es ;
      %put NOTE: No report will be generated because all variables in %data(&indata) were found in file %trim(%left(&file)). ;
     options nonotes ;
   %end ;
   %else %do ;
      proc print data=macro.missed label ;
         id name ;
         var data file ;
      run ;
     options ¬es ;
      %put NOTE: Macro FILESCAN printed a report of variables in %data(&indata) not found in file %trim(%left(&file)). ; 
     options nonotes ;
   %end ;

   proc datasets library=macro nolist ;
      delete missed contents sorted1 view1 tranny ;
   quit ;

   options nonotes ;
   %timenote (macro=filescan, starttime=&starttime)
   %put ;
   options ¬es ;
%mend filescan ;