//homepage‎ > ‎/software‎ > ‎test graphics‎ > ‎

# Precision in Stata

Contents (click to expand).

## 0.1  Create Data for Example.

First, create some fake data.

```.                 clear

. set obs 1000
obs was 0, now 1000

. g x = 1.1

. list in 1/5, noobs

+-----+
|   x |
|-----|
| 1.1 |
| 1.1 |
| 1.1 |
| 1.1 |
| 1.1 |
+-----+

. count if x ==1.1 // zero matches!!
0
```

## 0.2  Solutions

Let's look at how to deal with stored results on the fly. The hackish/kludgy solution we have used previously was to convert it to a string and take the substring to truncate the value. This is not ideal.

```.
.
.
.
.
. g   z = 999/_n

.  qui su z, d

.  di `"`r(mean)'"'
7.477985390007496

. di `"`=round(`r(mean)', 1.1)'"'
7.700000000000001

. di `"`=substr(`"`=round(`r(mean)', .01)'"', 1, 4)'"' //kludge using str
7.48
```

Instead, we should use one of the solutions below. These include using the extended macro function 'display' to properly format and / or round these values (SOLUTION 1) or create variables with proper display format (think of display format like a 'mask' over the true (and accurate) stored value) (SOLUTION 2).

```.
.
. **SOLUTION 1: use extended function format**
.  qui su z, d

.  di `"`r(mean)'"'
7.477985390007496

. local r:display  %3.2f `r(mean)'

. di `"`r'"'  //use stored result
7.48

. local r:display  %3.2f `=round(`r(mean)',.01)'

. di `"`r'"' //use calculated/rounded result
7.48

. g mean = `r(mean)'

. local r: display %3.2f `=mean'

. di `"`r' vs. `=mean'"' //use stored variables
7.48 vs. 7.477985382080078
```

.

```.
. **SOLUTION 2: create precise, formatted variable or scalar**
.  qui su z, d

. g  double p1 = `r(mean)'

. di %3.2f `=p1'  //display without macro extension
7.48

.
. l p1 in 1

+-----------+
|        p1 |
|-----------|
1. | 7.4779854 |
+-----------+

. *fix display format:
. format p1 %3.2f

. l p1 in 1 //fixed

+------+
|   p1 |
|------|
1. | 7.48 |
+------+

.
```

Instead of macros or variables, we can also work with lightweight -scalar-s to get the same result.

```. *note:
. scalar s1 = `r(mean)'

. di  %3.2f  s1
7.48

. di s1
7.4779854

.  assert `=s1' == p1    //true
```