Announcement

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

  • Example of local macro

    Dear All,

    my question is regarding to the following paragraph in the documentation:

    Click image for larger version

Name:	macro_definition.png
Views:	1
Size:	106.0 KB
ID:	1715794


    Specifically, can anyone illustrate any possibility to use curly braces with the local macros? (let's say to clarify meaning).

    I have tried the following, which doesn't seem to be successful:
    Code:
    local t=9999
    
    display `t'
    display `{t}'
    display {`t'}
    Results in:
    Code:
    . local t=9999
    
    . 
    . display `t'
    9999
    
    . display `{t}'
    
    
    . display {`t'}
    invalid syntax
    r(198);
    
    end of do-file
    
    r(198);
    Use of curly braces with global macros has been illustrated in the manual, but not with the local macros, afaik.

    Thank you very much, Sergiy

  • #2
    There is no nested reference here. However Sergiy Radyakin has a great point. I have seen examples in which braces are needed to insist on the order of evaluation, as in

    Code:
    . local j 1
    
    . global i1 foo
    
    . di "${i`j'}"
    foo
    where the braces insist on the order of evaluation, and


    Code:
    $i`j'
    would mean something quite different (even if it was nothing). This is the kind of example occasionally given, but avoidance of global macros is enough to make its personal use a matter of never.

    I don't recollect ever seeing braces used with two or more local macros.

    Comment


    • #3
      I think this is related to your previous question. The Programming manual shows the use and differences of local and global macros. The way I interpret it, along with the examples, is that Stata does not allow curly braces ({}) to work with local macro names. I believe that Stata would not be able to disambiguate which local or global macro reference is intended if curly braces could be used for nested macro expansion.

      Nick's example highlights the order of precedence used to evaluate global and local macros (Chapter 18.3.10).

      Indeed, Stata already allows nesting of local macros within local macros by simply nesting single quotes (`'), and nesting local macros within global macros (Nick's first example), as described in Chapter 18 of [U] Programming. The same principles apply for nested global macros. So Stata has all the syntax is needs for disambiguation between local and global namespaces for how to resolve macro substitution.

      Comment


      • #4
        This is the kind of example occasionally given..
        this example makes perfect sense, but the braces are attached to the global.

        I don't recollect ever seeing braces used with two or more local macros.
        Stata does not allow curly braces ({}) to work with local macro names
        From these observations I conclude that it is not possible to use curly braces with the local macros only, and their presence is only permitted in the context of global macros.

        Thank you very much, Nick and Leonardo!!

        Best, Sergiy

        Comment

        Working...
        X