Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Tip: Exponentiated macros should be placed in parentheses.


From   "D H" <dave1063@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   st: Tip: Exponentiated macros should be placed in parentheses.
Date   Mon, 11 Feb 2008 12:48:38 -0800

Stata contains a trap for novice programmers and users of certain
commands such as nl.  If a local or global macro contains a value that
is less than zero, care should be taken when using the exponentiation
operator.

Example:
. global A = -5

. display $A^2
-25

Woops.  Apparently, exponentiation precedes negation.  Since a macro
is basically just text, Stata takes 5, squares it, then applies the
negative sign.  To do this properly, use parentheses:

. display ($A)^2
25

That's better.  Or, consider using a scalar:

. scalar B = -4

. display B^2
16

For reference, here is Stata's order of evaluation for all operators,
from first to last:

! (or ~),^,- (negation),/,*,-(subtraction),+,!=(or ~=),>,<,<=.>=,==,& and |.


This trap (feature?) may very well be documented, though I couldn't
find it with Google.  I may be missing something.  I am running
version 8.2.
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index