Announcement

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

  • Passing a local macro to a nested do-file?

    Dear all,
    I'm quite new to stata, and I was wondering whether I'd better use a global macro in the following situation. My program includes two sub-do files where `x' is used commonly inside. When the code is executed, an error occurs because I guess the local macro, x, isn't passed into the sub-do files. Would you recommend me to use global macro instead? Or would there be an easy way to solve this?

    Code:
            local x 1
            do "$project\program1.do"
            do "$project\program2.do"

  • #2
    No guessing needed here: that is what local means: local to a particular program where program has an extended meaning, certainly including do-files.

    https://www.stata-journal.com/articl...article=dm0102 may be accessible to you and https://journals.sagepub.com/doi/pdf...36867X20976340 should be accessible to you.

    In the first instance check out

    Code:
    help include
    rather than global macros. Alternatives include combining the do-files into one. On the face of it that would be simplest.

    Comment


    • #3
      Nick Cox Thanks for your reply. I'll read the two papers carefully!
      I wanted to leave the two do files separately because I need to run just either of two at times.

      Comment


      • #4
        Well, then you must make sure that each do-file has the definitions it needs.

        Comment


        • #5
          This is the basic structure of the solution Nick was proposing

          Code:
          //-------------- begin file main.do
          cd "project folder" // ofcourse you need to change this to the directory
          do program1.do // notice no need for $project
          do program2.do
          //-------------- end file main.do
          
          //--------------------  begin file common_pars.do
          local x 1
          local y "foo"
          local z "bar"
          //--------------------  end file common_pars.do
          
          // -------------- begin file program1.do
          include common_pars.do // notice no path here; that was taken care of by the cd in main.do
          // do wonderful stuff with these local macros
          // -------------- end file program1.do
          
          // -------------- begin file program2.do
          include common_pars.do
          // do even more wonderful stuff with these local macros
          // -------------- end file program2.do
          Alternatively you can pass content to a do-file. For example if you execute do program1.do "foo" then when Stata starts executing program.do it will create a local macro `1' containing "foo". You can use the args command to give these macros names that are more representative of their content.

          Code:
          //-------------- begin file main.do
          cd "project folder" // ofcourse you need to change this to the directory
          do program1.do 2 "foo"
          do program2.do 2 "foo"
          //-------------- end file main.do
          
          // -------------- begin file program1.do
          args some_number some_string
          // do wonderful stuff with these local macros
          // -------------- end file program1.do
          
          // -------------- begin file program2.do
          args some_number some_string
          // do even more wonderful stuff with these local macros
          // -------------- end file program2.do
          I would use the first method for common parameters that remain fixed throughout the project. I would use the second method when I want to repeatedly run the .do file with different the parameters. Maybe the dataset is stored in different files with the same basic structure, and I need to perform the same data preparation steps in all these different files. In that case I can collect all the data preparation steps in a single file and use the second method to feed it the file names. I could easily combine the two methods when necessary. Nothing stops me from also using include to get some common constant parameters for my project into the second method.
          ---------------------------------
          Maarten L. Buis
          University of Konstanz
          Department of history and sociology
          box 40
          78457 Konstanz
          Germany
          http://www.maartenbuis.nl
          ---------------------------------

          Comment


          • #6
            Nick Cox Thanks for the interesting articles. I've learned a lot, especially "populating a matrix" and -nullmat-, from https://journals.sagepub.com/doi/pdf...36867X20976340.

            Comment


            • #7
              As you have mentioned that section, please note some typos that will be fixed formally by an Erratum in the Stata Journal


              Erratum: Speaking Stata: Loops, again and again

              Nicholas J. Cox, Durham University, UK

              The last code example in Section 6.2 of this column (Stata Journal 20: 999--1015 [2020]) is slightly mangled.

              On p.1014 the first line of code would be better as

              unab list : price-gear_ratio

              and on p.1015 correspondingly the first row of the matrix displayed should be

              price 0.449

              followed by the rest of the matrix as displayed there.

              Comment


              • #8
                Maarten Buis Thanks for your answer as well! It is really helpful.

                Comment

                Working...
                X