2

I'm a newcomer to Stata after using R for a long time.

I'm trying to perform any action which involves a macro, such as a loop and it is constantly giving me an invalid name r(198) error.

For example, when I first start up Stata, I enter the following code into the command prompt:

local hello 123
di 'hello'

This results in:

'hello' invalid name    
r(198);

Similarly, I try:

forvalues i=1/5 {
    display 'i'
}

Which results in the same invalid name error.

I'm obviously doing something fundamentally wrong. I have tried putting the same commands into a do file and running them, but the same thing occurred.

I was wondering if it's something to do with how I'm running the code as this link alludes to, but nothing suggested there worked for me.

I'm running Stata remotely from a server, if that could affect anything.

Mach
  • 73
  • 2
  • 3
  • 9
  • 2
    Enclose your macros between `" "`. – Roberto Ferrer Apr 15 '14 at 23:38
  • 2
    And it seems you are not referencing your macros correctly. It should be something like ``display "`i'"``. – Roberto Ferrer Apr 15 '14 at 23:41
  • 1
    I can't believe I've been caught up on something as trivial as using ' instead of `. How embarassing.. thanks for pointing that out. It works fine now. – Mach Apr 15 '14 at 23:45
  • But note that is not the only problem. `display` expects a string or a scalar. Strings are to be put between quotes. So two problems there. – Roberto Ferrer Apr 15 '14 at 23:47
  • 1
    Only somewhat pedantically, I think technically it is "dereferencing" rather than "referencing". – dimitriy Apr 15 '14 at 23:58
  • 2
    Confusion on this score can often be traced to learners not starting at the source, i.e. http://www.stata.com/manuals13/u.pdf At 18.3.1 in which local macros are introduced the use of different quotation marks is explained. (Different marks are needed if only because local macro references can be nested; otherwise `'a'b'c'` would be hopelessly ambiguous.) – Nick Cox Apr 16 '14 at 08:28
  • @DimitriyV.Masterov I'd say a Stata programmer refers to macros and Stata (tries to) interpret those references. I don't think discussions of pointers, addresses etc. are exactly analogous to how Stata macros are interpreted. – Nick Cox Apr 16 '14 at 08:32
  • @Roberto Ferrer Not quite. Quotation marks are not needed to `display` local macros that contain numeric characters. – Nick Cox Apr 16 '14 at 08:34
  • Also, `display %4.3f _pi` and `display ln(1000)` illustrate many numeric uses of `display`with no uses of scalars, macros or strings. – Nick Cox Apr 16 '14 at 09:31
  • @Nick and Dimitriy, thank you both for your comments. – Roberto Ferrer Apr 16 '14 at 11:26

0 Answers0