Announcement

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

  • Generating macro containing name of current do file

    Hello,
    Is there a way to do the above?
    I'm doing parallel analysis, with duplicate do files, except for one number. say.
    local x = 5

    To save going through all the individual files, can I retrieve info from the do file name?

    Regards,

    Andrew

  • #2
    The answer to your question is: no

    However, if you have basically duplicate .do files with only minor changes then I often find it easier to pass arguments to the .do file.

    If you type in Stata

    Code:
    do some_do_file.do with some text
    Then Stata will execute the do-file some_do_file.do, but at the very beginning it will set the local macros 1 to "with", 2 to "some", 3 "text", and 0 "with some text".

    Say I have three datasets, and I want to perform the same initial operations on each. In that case I create two .do files:

    Code:
    *------------------- proj_anc01 ------------------------
    clear all
    use `1'
    
    make smart changes
    
    save `2', replace
    *-------------------------------------------------------
    
    *------------------- proj_dta01 ------------------------
    forvalues i = 1/3 {
        do proj_anc01.do dataset`i' new`i'
    }
    *-------------------------------------------------------
    This solves your problem, as you no longer have duplicate do files to search. What I find more important is that this is much easier to maintain. Say that what I thought was smart, actually wasn't all that smart (happens all the time), so I have to change proj_anc01. Now I only have to change 1 .do-file and it will work for all three datasets. If I had three duplicate do_files, one for each dataset, I would need to change all three, and a bug or inconsistency could easily creep in.

    If you are feeling fancy, you could change proj_anc01 to:

    Code:
    *------------------- proj_anc01 ------------------------
    args oldfile newfile
    clear all
    use `oldfile'
    
    make smart changes
    
    save `newfile', replace
    *-------------------------------------------------------
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Maarten Buis gives excellent advice.

      Here is another detail: As do files can call do files, and so on, the current do file doesn't have to be unique.

      Comment


      • #4
        Hello,
        Thanks for this advice.
        These would work within a single job. My problem is that I'm using Stata in Unix batch mode with huge data sets on a HPC. I need to run the different do files in separate jobs.

        The reason I asked this as I had do files where the only difference was, say, a year.
        Do file name : filename_2017.do.
        In the do file I had:
        local YEAR = 2017

        There were 20 of these files.

        I didn't update correctly the local command in one of the do files. Meaning that I've had to re-run a 27 hour mi job.

        It is easy to confirm correct file names by looking at a list. Much harder when looking within a file (even if local command is at very top).

        Maybe I could achieve what I'm seeking outside of Stata using some sort of macro within, I shudder...MS Word. Where I have loop that changes local YEAR = 2017, to local YEAR = 2018. And also changes the file name.

        Regards,

        Andrew

        Comment


        • #5
          It's still not clear why you need these to be different files. If you had one file, you could run it multiple times as different jobs, and pass the YEAR as an argument:

          shell$ stata do filename.do 2017 &

          should work; then set

          local YEAR=`1'

          inside filename.do

          Unless I'm missing something.

          Jeph

          Comment


          • #6
            Hello,
            I have separate files, as I was running each do file as a separate job on the HPC.

            I may be able to implement this approach using the parallel ado, where I gather I can run multiple do files simultaneously. But without having to 'launch' separate Stata programs.
            I've not tried that yet.

            My point still stands in that it would be useful say, to be able to record in a log file the do file being executed.

            Regards,

            Andrew

            Comment

            Working...
            X