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 <[email protected]>
To   [email protected]
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
-16

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'
16

--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
interspersed):

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

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:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
--------------------------------------------------------
Nicholas Winter 607.255.8819 t
Assistant Professor 607.255.4530 f
Department of Government [email protected] e
308 White Hall falcon.arts.cornell.edu/nw53 w
Cornell University
Ithaca, NY 14853-4601

*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/




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