DATA Step, Macro, Functions and more

Searching SAS code for keywords

Reply
Occasional Contributor
Posts: 6

Searching SAS code for keywords

We are upgrading systems and tables/variables are being deprecated which will potentally cause my code to fail.  Does anyone have a way to search all the SAS code in my folder for combinations of certain keywords?  

 

Thanks!

Super User
Posts: 22,843

Re: Searching SAS code for keywords

Posted in reply to gnesshunkin

Windows SEARCH will do that or a Unix line command. IMO those are faster and more appropriate for this type of request. 

 

That being said, this question was asked on here a few weeks ago so if you search on here there's at least one full solution to searching all files in a folder for specific words.

Super User
Posts: 9,560

Re: Searching SAS code for keywords

Posted in reply to gnesshunkin

If your codes are stored on UNIX, a find with -exec grep will do the trick.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Trusted Advisor
Posts: 1,826

Re: Searching SAS code for keywords

Posted in reply to KurtBremser

I have posted next code few months ago. 

You need supply your argumnet as macro variables in first 3 lines of the code.

I use it in linux but believe it can be used in windows too. Try it:

%let search_string = rename;
%let suffix = sas;
%let root=/folders/myshortcuts/My_Folders/;
filename finp ("&root.sas_help/*.&suffix"); 

data results;
     length fname _filepath $200;
     infile finp filename = _filepath eov=_eov truncover;
     input a_line $200.;
     fname = _filepath;
     
     if _eov=1 then do;
       _n=0;

       _eov=0;
     end;
     _n+1;
     
     if find(a_line,"&search_string",'i')
     then output;
     keep _n a_line fname;
run;     
   
SAS Employee
Posts: 490

Re: Searching SAS code for keywords

Posted in reply to gnesshunkin

On UNIX you can do something like this:

 

find /path/to/folder/ -regextype posix-egrep -regex ".*\.(sas)$" 2> /dev/null | xargs grep -i 'keyword'
Super User
Posts: 9,560

Re: Searching SAS code for keywords

Actually, my version looks a little simpler:

find /path_to_folder -name \*.sas -exec grep -i keyword {} \; -print > keyword_prog.lst
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 5

Re: Searching SAS code for keywords

Posted in reply to gnesshunkin

Set up Microsoft File Explorer find keywords in a SEARCH of .sas files

When you use Windows File Explorer to SEARCH for keyword they don't show up. The search may not include files of with certain extensions! In fact, quite a few file extensions/types will be omitted. Microsoft created this situation deliberately, to avoid having you find "irrelevant" files and SAS was left out from this list. Updating the Windows Registry will rectify this and allow Windows to search the file types you need.

The Search relies on an internal filter to look at a limited group of files Those file extensions that don't have a defined HKEY_CLASSES_ROOT filter won't be searched. If you find that certain file extensions don't appear in the search results, you can try assigning the plain-text filter to those types.

To have Windows File Explorer search files of a certain file extension in this case .SAS:

1. launch Regedit from the Start menu's Run dialog.
2. navigate to the key HKEY_CLASSES_ROOT\.??? (where .??? is the extension you want searched - sas).
a. If you don't find a subkey named PersistentHandler, create one.
3. double-click on the value "(Default)" for that subkey to set its value to {5e941d80-bf96-11cd-b579-08002b30bfeb}

• Administrator rights are needed to perform this.

Ask a Question
Discussion stats
  • 6 replies
  • 150 views
  • 2 likes
  • 6 in conversation