Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Searching 1) for user written commands and 2) searching my .do files

    Hello
    I have 2 questions about searching in Stata
    1) which is the best way to search the user/community written programs or .ado file for a given subject or keyword? I presume searching the Stata help files just searches the Stata supplied commands.
    2) is there a way to search the folder containing my own .do file? My usual search is for a program and I cant remeber which .do file it's in. The Windows search doesnt work.
    Sorry if these are simple questions but I can't find an answer to them easily.
    Thanks, Jane

  • #2
    For your first question, see the output of the Stata command help search.

    I'm afraid I can't address your second question; I'm not a Windows user.

    Comment


    • #3
      1) Not so. Just reading the help for search contradicts that presumption.


      search searches a keyword database and the Internet for Stata materials related to your query.
      The help and manual entry say much more.

      2) I don't know what "the Windows search" means precisely, or why it doesn't work, but you can search a folder in Windows or from within Stata.

      Community-contributed programs will in practice be defined in .ado files, so suppose you want to look in d:\my big folder\my little folder for *.ado files, you can do that using these Stata commands:

      Code:
      cd "d:\my big folder\my little folder"
      dir *.ado
      dir someth*.ado
      The detail that paths including spaces must be specified within double quotes is worth flagging. The example supposes that you are looking for names like someth followed by any other characters.


      Comment


      • #4
        Originally posted by Jane Burnell View Post
        Hello
        I have 2 questions about searching in Stata
        1) which is the best way to search the user/community written programs or .ado file for a given subject or keyword? I presume searching the Stata help files just searches the Stata supplied commands.
        2) is there a way to search the folder containing my own .do file? My usual search is for a program and I cant remember which .do file it's in. The Windows search doesn't work.
        Sorry if these are simple questions but I can't find an answer to them easily.
        Thanks, Jane
        1) No, if you type in "search merge" Stata will return official Stata commands first (append, joinby, merge, etc), but if you keep scrolling down you will get to "Web resources from Stata and other users" which will list a number of community-contributed packages that might be of interest (for example, addinby, dmerge, matchit, mergeall, mergemany, etc)
        • If you want to jump straight to community-contributed packages, within Stata go to Windows > Search and then click the "Search net resources" before entering the keyword. (It's the equivalent of typing "search merge, net" in Stata's interactive window).
        • You can also go to https://ideas.repec.org/s/boc/bocode.html and type your search there (although it only searches user-contributed packages on SSC).

        2) I haven't found a convenient way to search for commands within my own do files. (i.e. "in which do file did I reshape the data from wide to long?").
        a) If the question is "I did a loop like that in the past, where is it so I can do it again?" I keep a list of code snippets that I frequently use in software called Evernote (other people use OneNote, Google Keep, Github, etc for such purposes).
        b) If the question is, "I have eight do files working with the data on my current project, in which one did I merge in data on the firm patents?" I haven't found a good answer for Windows. (I use Windows 10, Stata 15.1).

        Comment


        • #5
          #1 asked about searching a folder. If you want to search a .do file for mentions of a program, you need a text editor or some utility like find from the OS Command Prompt, but doedit is a text editor. The program name will often be the first word on a command line; that may help.

          Comment


          • #6
            Thank you for your replies.
            1) I now understand how search will find user written commands
            2) this is like the 2b above. I'll try a text editor.

            Comment


            • #7
              For 2) I strongly recommend using Notepad++. It allows you to "find in files". Just type in the phrase you are looking for (e.g. program define veryUsefulProgram), select the folder where you want to search in and you'll have a result before you can get a coffee. You can also filter on .do extensions, include regular expressions, ...

              Comment


              • #8
                Actually, you can write a program that will read all the files in the folder and identify which ones contain your keyword. It's not hard, but it requires that you install two of Robert Picard's programs: -filelist.ado- and -runby.ado- (the latter co-authored with me), both available from SSC.

                I use this all the time:

                Code:
                clear*
                
                filelist, pattern(*.do)
                
                capture program drop one_file
                program define one_file
                    local file `"`=dirname[1]'/`=filename[1]'"'
                    gen strL content = fileread(`"`file'"')
                    gen long foundit = strpos(content, "whatever")
                    drop if !foundit
                    exit
                end
                
                runby one_file, by(dirname filename)
                Notes:

                1. If the folder contains subfolders, this code will go through all of the subfolders. If you don't want that, add the -norecursive- option to the -filelist- command.

                2. When you are done, the data set in memory will consist of the names and locations of those files that contain the string "whatever", along with the location in that file (# of characters from beginning of the file, in the variable foundit) where that string is found.) Evidently, replace whatever by the actual string you are trying to find.

                3. If the files contain Unicode characters then you need to modify the code accordingly.

                4. The code can be modified to do a global find and replace of a string in all the files where it occurs by just adding a -replace content = subinstr(content, "whatever", "replacement_string", .)- and then a -filewrite(`"`file'"', content, 1)- just before the -exit- command in program one_file.

                Comment

                Working...
                X