Announcement

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

  • Issue with liocal macros

    Dear All,

    I am trying to generate some local macros in my do file. For instance in my do file I have:

    Code:
    local impower "2"
    Then I want to see the content of the macro. So in the command window I type

    Code:
    display "`impower'"
    obtaining this:

    Code:
    . do "C:\Users\dario\AppData\Local\Temp\STD9ef8_000010.tmp"
    
    . local impower "2"
    
    .
    end of do-file
    
    . di "`impower'"
    
    .
    If I generate the local macro from the command window, I get:

    Code:
    . local impower "2"
    
    . di "`impower'"
    2
    Why this happens? I guess I am doing a mistake but I cannot figure out which one. Any help you may want to provide would be highly appreciated.

    Best,

    Dario

  • #2
    Hi Dario, the reason is that you have run a snippet your code in a separate instance from when you later try to use that macro in interactive Stata. When Running whole do-files, or even when running snippets of code from the do-file editor, these are treated as isolated instances of Stata as it relates to local and global macros. This code will work if you execute the whole block that includes the local macro definition and the later bit of code that uses it.

    Comment


    • #3
      The sting is in the definition. A local macro defined within a do-file is not visible outside the operation of that do-file. Indeed, once the do-file has finished the local macro no longer exists.

      Conversely, when you define a local macro within an interactive session that local macro remains visible within that session, unless you destroy it or until the session ends.

      In short, local means within the current program space, where a program space is defined by the do-file being executed, the do-file editor contents being executed, the current program being executed, or the main interactive session.

      More at

      SJ-20-2 dm0102 . . . . . . . . . Stata tip 138: Local macros have local scope
      . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
      Q2/20 SJ 20(2):499--503 (no commands)
      focuses on a common misunderstanding of how local macros work

      https://journals.sagepub.com/doi/pdf...36867X20931028

      Comment


      • #4
        Ok. Thanks. it is clear now. I have a follow up question. This is what I am trying to do in my do file:

        Code:
        * Define user inputs
        local endog "pc_culture"
        local instruments "pc_institutions literacy1880"
        local controls "urb_rate1850 school country1 country2 country3 country4 country5 country7 country8 country15"
        
        * Combine instruments and controls
        local exog "$instruments $controls"
        
        * Create spatial lags up to impower(2)
        
        foreach v of local exog{
            spgenerate W1_`v' = dW1500 * `v'
            spgenerate W2_`v' = dW1500 * W1_`v'
        }
        But when the loop arrives, I get an error message "invalid syntax".

        I cannot understand why.

        Best,

        Dario

        Comment


        • #5
          The problem is that you define -local exog "$instruments $controls"-. But $instruments and $controls are references to global macros named instruments and controls. But you have never defined any global macros in the code. You defined instruments and controls as local macros, so you must refer to them the way local macros are referenced:

          Code:
          local exog `instruments' `controls'

          Comment


          • #6
            In principle globals called instruments and controls might have been defined before the do-file was written.

            In practice I would go with your Clyde Schechter 's diagnosis, as it is hard to see that the story just given would account for the error.

            Comment


            • #7
              Thanks all for your answers. I tried what Clyde Schechter suggested. But still I get this:

              Code:
              . do "C:\Users\dario\AppData\Local\Temp\STD9ef8_00001w.tmp"
              
              . local endog "pc_culture"
              
              . local instruments "pc_institutions literacy1880"
              
              . local controls "urb_rate1850 school country1 country2 country3 country4 country5 countr
              > y7 country8 country15"
              
              . 
              . * Combine instruments and controls
              . local exog `instruments' `controls'
              
              . 
              . * Create spatial lags up to impower(2)
              . 
              . foreach v of local exog{
                2.     spgenerate W1_`v' = dW1500 * `v'
                3.     spgenerate W2_`v' = dW1500 * W1_`v'
                4. }
              invalid syntax
              r(198);
              
              end of do-file
              I also tried to trace the executing of the lines using set trace on, but I simply get invalid syntax. I cannot understand where the issue is with the syntax.

              Best,

              Dario

              Comment


              • #8
                I found the issue! If I type:

                Code:
                spgenerate W1_`v' = dW1500 * `v'
                I get invalid syntax. Instead, if I type:

                Code:
                spgenerate W1_`v' = dW1500*`v'
                It goes through. The issue is with the space between dW1500, * and `v'.

                Comment


                • #9
                  #8 I would need your data to be completely confident but on the face of it the command that didn't work and the command that did work look equivalent to me. I suspect a different explanation, that somehow you had invisible characters that were confusing Stata. Ascii 160 for example often looks like a space but is not your common or garden space.

                  Comment

                  Working...
                  X