Announcement

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

  • Converting do/ado files into text by code

    Hi all;

    I have been using STATA in the past couple of years and developed lots of codes so far in both do and ado files. I wanted to see how I can convert the ado/do files into txt files (not manually by copying and pasting but saving or exporting if they work for do/ado files). The bigger purpose of this work is that I would like to analyze all my codes to see the frequency of each command I used. I think that can be best done by converting the do/ado codes into texts, reading txt files, and counting each command entered as string variables.

    Does anyone know how I can convert do/ado file codes into txt?

    Thanks

  • #2
    do and ado files are usually (plain) text files ... The file extension should not be relevant to reading the file with whatever software you choose. Stata has file command that does this. It is going to be pretty hard to automatically find commands in those files, though.

    Comment


    • #3
      For commands, I will have a separate command spreadsheet to apply making the finding of commands easier.

      How can I save a plain text do/ado file as *.txt. It seems the 'file' command does not work for do/ado files. Here is the quasi-code I am seeking for:

      file open `myfile.do/ado'
      save "${path}/myfile.txt", replace

      The easier option is to copy and paste all the text from the do/ado file into a notepad and save it as txt. But I wish to avoid it as there are many ado/do files I have, and I want to avoid doing this process for every single do/ado file.

      Thanks

      Comment


      • #4
        You can try findfile to locate the do/ado file and copy to change the extension.
        file is OK too but it will require opening and reading the do/ado unless you are looking for a pure Mata solution.

        Comment


        • #5
          Why do you even want .txt extensions in the first place? This code reads and prints to screen regress.ado line by line:

          Code:
          findfile regress.ado
          tempname fh
          file open `fh' using "`r(fn)'" , read
          file read `fh' line
          while ( !r(eof) ) {
              display `"`line'"'
              file read `fh' line
          }
          file close `fh'
          Last edited by daniel klein; 04 Jan 2021, 15:27.

          Comment


          • #6
            Originally posted by daniel klein View Post
            Why do you even want .txt extensions in the first place? This code reads and prints to screen regress.ado line by line:

            Code:
            findfile regress.ado
            tempname fh
            file open `fh' using "`r(fn)'" , read
            file read `fh' line
            while ( !r(eof) ) {
            display `"`line'"'
            file read `fh' line
            }
            file close `fh'
            I think you should try the type command, it achieves the same thing with less code (although it prints the output with di as res) :-)

            Comment


            • #7
              I am sorry for obviously (still) not being clear enough.

              I was not trying to show how to print the code for regress.ado to the screen. That was merely a silly example. My point is that there is really no point in changing .ado extensions to .txt extensions, which is what Alireza Mahdavi asked for. Doing that is simply a waste of time. File extensions are irrelevant to what Alireza Mahdavi wants to do. Stata (and any other software) is very happy with reading (plain) text files regardless of whether their extension is .txt,.ado, .foo., or anything else. My post in #5 demonstrates that. I did not change the file extension and I am still able to read the file. If I wanted to, I could, instead of printing the lines to the screen, find any commands that are called in regress.ado, which is what Alireza Mahdavi wants.

              Comment


              • #8
                To Daniel's quite correct points, I would add that even if do and ado files weren't plain text files, one could use Stata's -fileread- command to read their contents into strL variables and investigate them. (-fileread- reads files as binary.) Here's an illustration of an approach relevant to Alireza's apparent goal:
                Code:
                clear
                cd YourDoFileDirectory
                local dofiles: dir . files "*.do"
                // Read names and contents files into numbered variables on a single observation.
                set obs 1
                local i = 0
                foreach f of local dofiles {
                   local ++i
                   gen str name`i' = `"`f'"'
                   gen strL file`i' = fileread(name`i')
                }
                // Per usual, long format is more convenient.
                gen byte dum = 1 // to satisfy reshape
                reshape long name file, i(dum) j(fnum)
                drop dum
                //  Each observation contains the name and contents of a file.
                //  For example: How many files contain "regress,"  "local," and "reshape" ?
                local clist = "regress local reshape"
                foreach c of local clist {
                   gen byte has_`c' = (strpos(file, "`c'") > 0)
                }



                Comment


                • #9
                  At the risk of spelling out the obvious, let's flag some of the difficulties here:


                  1. Stata allows abbreviations of many of its own commands (and community-contributed commands can do that too).

                  2. There is not, and I suspect cannot be, a complete list of community-contributed commands, even those public in some sense.

                  3. A command isn't necessarily the first word on a physical line.

                  4. In extension of #3 prefix commands complicate parsing.

                  Comment

                  Working...
                  X