Trouble with format command changing the value

Viewed 26

I first applied destring to an ID variable (with 17 digits). They are destrung but then they are shown in scientific notation. So I tried the command format %20.0f. Now all digits are shown but the last 2-3 digits are now changed.enter image description here

2 Answers

Stata can only hold numeric variables with up to 16 digits.

Your best option is probably to keep the ID as a string.

The command format only affects how a data point is displayed to humans, not how it is actually stored.

This is to complement the answer by @TheIceBear.

format never changes values. The problem is that your string is too big even for its numeric equivalent to be held exactly in a double, except occasionally.

clear 
set obs 5
gen id = 17*"9" in 1 
replace id = 16*"9" + "6" in 2 
replace id = 16*"9" + "2" in 3 
replace id = 15*"9" + "88" in 4 
replace id = 15*"9" + "84" in 5 
format id %20s 
destring id, gen(nid)
format nid %20.0f 
list  

     +----------------------------------------+
     |                id                  nid |
     |----------------------------------------|
  1. | 99999999999999999   100000000000000000 |
  2. | 99999999999999996   100000000000000000 |
  3. | 99999999999999992   100000000000000000 |
  4. | 99999999999999988    99999999999999984 |
  5. | 99999999999999984    99999999999999984 |
     +----------------------------------------+
Related