Announcement

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

  • Local Macro Resolution Confusion

    Hi,

    I've been reading about local/global macro resolution in Stata. My question relates to this document https://www.stata.com/manuals/pmacro.pdf, header "Macro assignment" and text
    "
    The compound double quotes (‘” and ”’) are needed when something itself contains quotation marks. In fact, if the string is anything more complex than a single word, it is safest to enclose the string in compound quotes (‘” ”’). The outermost compound quotes will be stripped, and all that remains will be assigned to name. Note that any embedded macro references in something are expanded before assignment to name whether or not compound quotes are used
    "

    On this basis, I understand that the code
    Code:
    local file `"C:/Users..."'
    Would mean that the macro file contains "C:/Users..." (quotes not to be ignored)

    Therefore, I would presume that code below would work...
    Code:
    display `file'
    but it doesn't. It returns "C: invalid name"

    I find this confusing because the code below does work

    Code:
    display "C:/Users..."
    
    [Output:] C:/Users...
    What's wrong? Based on the error, it seems that the command display also unresolves the quotes from file and thinks that it is a variable or something. I suspect this because even the former, hard-coded command only prints C:/Users... and does not include the quotes.



    Thank you for helping.

  • #2
    Code:
    local number = 3
    local string = "Stata"
    
    di `number'
    di "`string'"
    di `string'

    Comment


    • #3
      Compound double quotes are not an outer shell of ` ' and an inner shell of " ". They are both together, just as == >= <= are all single operators that each happen to be typed as two characters.

      Comment


      • #4
        The problem in #1 doesn't seem to be about embedded quotes. It's about a misunderstanding of (plain) double quotes as delimiters.

        Delimiters show where strings begin and end. They are not part of the strings, any more than a box that holds a pen is part of the pen.

        Let's back up.

        Code:
        local foo1 frog
        local foo2 "frog"
        local bar1 toad newt
        local bar2 "toad newt"
        have the same effects. The " " are delimiters that are stripped on defining a local macro. In these examples the delimiters do no harm and make no difference are thus optional.

        But delimiters " " are needed for strings in other contexts. They can have an essential role in tying strings together.

        If I go

        Code:
        di   `foo1'
        two things happen in sequence. First the local macro is evaluated. Then display tries to display its argument after that evaluation.

        Code:
        di frog
        and that will work if (and only if) frog is the name of a scalar or the name of a variable (in which case display will show the value of frog[1], the value in the first observation -- that's another story!)

        On the other hand if I go

        Code:
        di "`foo1'"
        that is (crucially!) different. Again, two things happen in sequence. First the local macro is evaluated. Then display tries to display

        Code:
        di "frog"
        which is fine. display sees there a literal string frog and does what it is told.
        Last edited by Nick Cox; 18 Jul 2025, 01:20.

        Comment


        • #5
          I think the critical thing to understand is that display evaluates its argument. The quotes are crucial to the argument being interpreted as a literal string. To illustrate:
          Code:
          . display 4+3
          7
          
          . display "4+3"
          4+3
          
          . display hello
          hello not found
          r(111);
          
          . display "hello"
          hello
          
          . local file "hello"
          . display `file'
          hello not found
          r(111);
          
          . local file hello
          . display `file'
          hello not found
          r(111);
          
          . display "`file'"
          hello
          The error obtained by OP is the same kind of thing that is happening in the third, fifth and sixth display commands above (the three are essentially the same, as explained by Nick in #4 above, and the error is because there is no scalar or variable in memory called hello, again as explained in #4).
          Last edited by Hemanshu Kumar; 18 Jul 2025, 02:35.

          Comment

          Working...
          X