Announcement

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

  • "invalid name" error in macro assignment

    A simple error is driving me nuts. Can anyone see what I'm doing wrong?
    Code:
    global effect_ladder 0 0.15 0.3 0.45
    power oneway $effect_ladder, alpha(0.05) grweights(1 1 1 1)
    global $students_simple=`r(N)'
    The error is
    = invalid name

  • #2
    It is unclear what you are trying to do here.

    If you are trying to create a new global macro whose name will be students_simple, then the error is the use of the $. It should just be -global students_simple = `r(N)'-. The $ is used (in most situations) when you access the contents of a global macro, not when you create it.

    If you already have a global macro named students_simple that contains some other content, let's just say it contains "xxx" for illustration, and you now want to create a global macro named xxx that contains the value of r(N) from the preceding command, then the problem is that Stata thinks that the global macro's name is not students_simple but is students_simple=`r(N)', which is not a legal macro name (only letters, digits, and _ are allowed). So to fix this you have to let Stata know where your intended name ends. You can do this in either of two ways. You can put a blank space after simple: -global $students_simple = `r(N)'-, and this will then set global xxx = `r(N)'. Or you can enclose students_simple in curly braces: -global ${students_simple}=`r(N)'-.

    All of that said, I will remind you that global macros are an inherently unsafe programming practice and should be used only when there is no good alternative. For the purpose of holding simple lists or returned results it is better to use local macros. An additional advantage of local macros that shows up in the context of this particular problem is that there is never any ambiguity for Stata about where the name ends when you reference it, because the name of the macro is always delimited by ` at the beginning and ' at the end.

    Comment

    Working...
    X