Title | The use of macros with the power operator | |
Author | William Gould, StataCorp |
Be careful using macros with the power operator. Never code
... `a'^2 ...
Instead, code
... (`a')^2 ...
In particular, macros will surprise you when they contain negative numbers.
. local a = -2 . display `a'^2 −4
To understand why this occurred, remember that macros substitute and think about what the line looked like after substitution.
What do you think −22 should equal, 4 or −4? That depends on whether you take −22 to mean (−2)2 or −(22). Most people agree that −22 means −(22) and that the "right" answer is −4. Stata agrees with that:
. display -2^2 −4
You may agree that −22 = −4, but if a = −2, it is obvious that a2 should be 4 and that a2 should be 4 regardless of the sign of a. Stata agrees:
. input a a 1. -2 2. 2 3. end . generate b = a^2 . list +--------+ | a b | |--------| 1. | -2 4 | 2. | 2 4 | +--------+
The problem is that macros such as `a' are not variables; they are substitutes. When you type "...`a'...", the contents of `a' are picked up and dropped right in its place in what you typed.
If you type `a'2 and `a' contains −2, then the result is −22, which, as we previously agreed, is −4.
The programming rule I use is always to bind `a' in parentheses — (`a') — when taking powers, and I do that whether `a' is a macro or a scalar. With scalars it is unnecessary; scalars are variables and negative two is stored whole in it:
. scalar c = -2 . display c^2 4
but I bind my scalars in parentheses anyway because I sometimes forget whether `a' is a macro or a scalar.
The danger of using `a'2 rather than (`a')2 did not exist in Stata 4 and earlier; it does exist in all modern Statas.
Older Statas took −22 to be (−2)2 or 4, and so coding `a'2 was perfectly safe. Interactive users, however, were shocked by this behavior, so we changed the way Stata works, and we made the change under version control so that old programs and ado-files continue to work:
. display -2^2 −4 . version 4.0 . display -2^2 4
As a programmer, I preferred Stata’s old way of working. That was, however, insufficient reason, because the old way made −22 produce 4, and that surprised interactive, nonprogramming users. The new way puts the confusing part back where it belongs, on programmers.
Learn
Free webinars
NetCourses
Classroom and web training
Organizational training
Video tutorials
Third-party courses
Web resources
Teaching with Stata
© Copyright 1996–2024 StataCorp LLC. All rights reserved.
×
We use cookies to ensure that we give you the best experience on our website—to enhance site navigation, to analyze usage, and to assist in our marketing efforts. By continuing to use our site, you consent to the storing of cookies on your device and agree to delivery of content, including web fonts and JavaScript, from third party web services.
Cookie Settings
Last updated: 16 November 2022
StataCorp LLC (StataCorp) strives to provide our users with exceptional products and services. To do so, we must collect personal information from you. This information is necessary to conduct business with our existing and potential customers. We collect and use this information only where we may legally do so. This policy explains what personal information we collect, how we use it, and what rights you have to that information.
These cookies are essential for our website to function and do not store any personally identifiable information. These cookies cannot be disabled.
This website uses cookies to provide you with a better user experience. A cookie is a small piece of data our website stores on a site visitor's hard drive and accesses each time you visit so we can improve your access to our site, better understand how you use our site, and serve you content that may be of interest to you. For instance, we store a cookie when you log in to our shopping cart so that we can maintain your shopping cart should you not complete checkout. These cookies do not directly store your personal information, but they do support the ability to uniquely identify your internet browser and device.
Please note: Clearing your browser cookies at any time will undo preferences saved here. The option selected here will apply only to the device you are currently using.