# Re: st: Inconsistent Result of generating variables

 From Richard Williams To statalist@hsphsun2.harvard.edu, Subject Re: st: Inconsistent Result of generating variables Date Fri, 09 Apr 2004 18:26:47 -0500

```At 05:12 PM 4/9/2004 -0400, Jitian Sheu wrote:
```
```Hello, listers:

I am not sure whether it is a bug in Stata?

Before going on, I will like to post my code first, then that would be
easier to explain:

local b01=0.02

local b11=0.3

local b21=-0.25

gen tmptau1=emusq1+`b01'^2+(`b11'^2)*(x1^2)+(`b21'^2)*(x1^4)-2*`b01'*emu1-2*
/*
```
Those are complicated expressions! But the problem, I believe, is that b21 is a negative number. There is a FAQ on this somewhere, but I don't recall where it is. Basically, in the b21 part of the expression above, what you probably want is

-.25^2 * x1^4 = (-.25)^2 * x1 ^ 4 = .0625 * x1 ^ 4

but what Stata is giving you is

-(.25^2) * x1^4 = -.0625 * x1^4

i.e. you are subtracting when you want to add, hence your first measure always winds up smaller than your second (even when they appear the same, they are different several decimal places down).

To fix this, in the above line, put another set of parentheses around 'b21', i.e. make it

```gen tmptau1=emusq1+`b01'^2+(`b11'^2)*(x1^2)+((`b21')^2)*(x1^4)-2*`b01'*emu1-2*
```
When I do that, I get

. list tmptau1 tmptau1_alt

+---------------------+
| tmptau1 tmptau~t |
|---------------------|
1. | .8692592 .8692592 |
2. | .2181771 .2181771 |
3. | .2075188 .2075188 |
4. | .1309747 .1309747 |
5. | .6345458 .6345458 |
|---------------------|
6. | .4080512 .4080512 |
7. | .2966675 .2966675 |
8. | .3682937 .3682937 |
9. | .0604092 .0604092 |
10. | .9241688 .9241688 |
+---------------------+

Incidentally, Stata is doing it right; exponentiation gets done before subtraction. e.g. if I punch in 3 - 4^2 on my calculator, I get -13, not 19. If I punch in 3 + (-4)^2, I get 19. But, it is easy to get confused about!

*
* 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/