Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: St: Programming question, local macros

From   Nick Winter <>
Subject   Re: st: St: Programming question, local macros
Date   Wed, 22 Sep 2004 09:59:40 -0400

This comes up periodically. You can get the same behavior this way:

. di -4^2

What's happening is that Stata is evaluating your expression according to its order of operations: first the exponentiation, then the subtraction.

It does the same thing when you are using the macro: first substitutes the contents of the macro, so "`x'^2" becomes "-4^2" and then "-4^2" is evaluated as above.

Remember, Stata's doesn't look at the macro reference `x' and say "aha, here is a number," it just blindly substitutes the string -4 for `x' and goes on. (Of course, this is one of the reasons macros are so powerful.)

So in general you want to do something like:

local xsq = (`x')^2

. local x = -4
. local xsq = (`x')^2
. di `xsq'

--Nick Winter

At 08:37 AM 9/22/2004 -0500, you wrote:

Could someone please explain the following behavior?  I'm running fully
up to date Stata 8.2 on Windows 2000.

In a program I have the following statements (with responses

. local x = -4
. display `x'
. local xsq=`x'^2
. disp `xsq'

I kind of expected xsq to have the value of 16 rather than -16.

Thanks in advance.
Dale Plummer
Department of Biostatistics
Vanderbilt School of Medicine
S-2323 Medical Center North
Nashville, Tennessee 37232-2158

*   For searches and help try:
Nicholas Winter 607.255.8819 t
Assistant Professor 607.255.4530 f
Department of Government e
308 White Hall w
Cornell University
Ithaca, NY 14853-4601

* For searches and help try:

© Copyright 1996–2021 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index