Announcement

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

  • Making systematic changes to set of do-files

    I have a large number of do-files that I would like to run by systematically changing parts of the code.

    Changes I want to make:

    Code:
    keep [varlist]
    changed to

    Code:
    keep [other varlist]
    as well as

    Code:
    save file_name
    changed to

    Code:
    save other_file_name
    Ideally, I would like to find a solution such that I can loop over all do-files, with the do-files being executed with the aforementioned changes

    Code:
    forvalues i =1/100 {
    
    [run with the mentioned changes] do_file_`i'
    
    }

  • #2
    You could read in each do-file as one string variable and make changes to that string variable and export accordingly.

    Comment


    • #3
      .... but much, much better technique is to allow your do-file to take arguments. That's all in https://www.stata.com/manuals/u16.pdf

      Comment


      • #4
        I agree with Nick that doing this via arguments given to do-files would be a nicer approach, but I can imagine a situation in which that would be onerous. I'm supposing that Antoine's do files are numerous, are already written, and vary sufficiently that making them into one do-file with small variations depending on the command-line arguments would not be easy. If this is the case, I think the "use a string variable" approach, while something of a hack, would be justified by the time saved. This approach would use the too-little known -fileread()- and -filewrite()- functions in Stata. I'm thinking of something like this (not tested, since I don't have good example material):

        Code:
        clear
        set obs 1
        gen strL dofile = "" // Probably needs to be a strL
        gen b = .
        local oldlist = "cat dog"
        local newlist = "cat dog frog"
        local filelist: dir "SomeDirectory" files *.do"
        foreach file of local filelist {
          replace dofile = fileread("`file'")
          // Spaces in oldlist would be a problem for subinstr(), as would varying
          // ordering of variables within oldlist, but that *could* be fixed here.
          replace dofile = subinstr(dofile, "`oldlist'", "`newlist'", .)
          tempfile temp
          replace b = filewrite("`temp'", dofile)
          di "*****"Running -`file'- with varlist = `newlist'*****", _newline(2)
          do "`temp'"
          erase "`temp'" // filewrite() won't overwrite
        
        }

        Comment


        • #5
          This is awesome. Thank you so much!

          Comment

          Working...
          X