# st: RE: RE: Mathematical precision question

 From "Nick Cox" <[email protected]> To <[email protected]> Subject st: RE: RE: Mathematical precision question Date Wed, 30 Jan 2008 14:17:39 -0000

```Offlist I got a suggestion that more detail would help. (I just had my
coat on to go home when the question came in.)

. gen ddy = string(Var1) + string(Var2, "%03.0f") + string(Var3,
"%03.0f")

is a way of getting what you want. The result is a string variable. The
second argument of -string()- is a numeric format that ensures leading
zeros when appropriate.

Nick
[email protected]

Nick Cox

As you say, this is all about precision. -search precision- for some

. gen long ddy =

means that you will not get a -float- type, which is the default.

Alternatively, represent your identifiers using string variables, and
manipulate them by concatenation, just +.

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Lektzian,
David
Sent: 29 January 2008 21:20
To: [email protected]
Subject: st: Mathematical precision question

Dear Statalist,

I have a simple problem that I am hoping somebody can help me with.  I
am trying to generate a variable that multiplies three other variables
by quantities and then adds them.  Specifically, here is what I am
trying to do:
Var1 = 1972
Var2 = 2
Var3 = 20

I want to generate the variable ddy as follows:
.gen ddy = Var1*1000000 + Var2*1000 + Var3

The correct answer, both on my calculator and in excel is: 1972002020

So, what does Stata come up with?  Formatting the answer into the 10
digits necessary to view the answer:
.Format ddy %10.0f

1972002048

I'm sure this must be something to do with precision, but I don't know
how to fix the problem.  When I run this repeatedly, Stata somewhat
INconsistently gets this operation wrong every time.  In the first
example Stata was over by 28.  In this next example Stata is under by
36.
Var1   Var2   Var3
1973   002     020 = 1973001984 according to Stata when in reality it
equals 1973002020

Here Stata is closer only going over by 7.
Var1   Var2   Var3
2002   002     055 = 2002002048 according to Stata when in reality it
equals 2002002055

I'm sure this problem must have something to do with precision, but I
need these answers to be exact.  Can somebody help explain to me how to
get Stata to perform these mathematical operations correctly?

*As a note, I will mention that I am using these variables as ID
variables which is why I want them exact.  There might be a better way
to create ID variables, but I'm a bit of a novice with Stata syntax.

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