Home  /  Resources & Support  /  Introduction to Stata basics  /  How to round numeric data

Numeric data are sometimes stored with many decimal places of accuracy. Sometimes, this level of accuracy is unnecessary, and we would like to round the data or, possibly, use a floor or ceiling function.

Let's begin by opening and describing an example dataset from the Stata website.

. use https://www.stata.com/users/youtube/rawdata.dta, clear

. describe

Contains data from https://www.stata.com/users/youtube/rawdata.dta
 Observations:         1,268                  Fictitious data based on the
                                                National Health and Nutrition
                                                Examination Survey
    Variables:            10                  6 Jul 2016 11:17
                                              (_dta has notes)
Variable Storage Display Value
name type format label Variable label
id str6 %9s Identification Number age byte %9.0g sex byte %9.0g Sex race str5 %9s Race height float %9.0g height (cm) weight float %9.0g weight (kg) sbp int %9.0g Systolic blood pressure (mm/Hg) dbp int %9.0g Diastolic blood pressure (mm/Hg) chol str3 %9s serum cholesterol (mg/dL) dob str18 %18s
Sorted by: id

The variable label for weight tells us that it is measured in kilograms. Let's list the first five observations in the variable weight.

. list weight in 1/5

weight
1. 83.69
2. 83.69
3. 64.98
4. 81.65
5. 92.65

Weight is measured with two digits to the right of the decimal. But we don't need the decimal values, so we may prefer to round weight to the nearest whole number. Let's use the generate command and the round() function to create a new weight variable rounded to one decimal place.

. generate weight1 = round(weight, 0.1)

. list weight weight1 in 1/5

weight weight1
1. 83.69 83.7
2. 83.69 83.7
3. 64.98 65
4. 81.65 81.7
5. 92.65 92.7

Or we could round weight to the nearest whole number.

. generate weight0 = round(weight, 1.0)

. list weight weight1 weight0 in 1/5

weight weight1 weight0
1. 83.69 83.7 84
2. 83.69 83.7 84
3. 64.98 65 65
4. 81.65 81.7 82
5. 92.65 92.7 93

Sometimes, we may wish to use a floor function rather than rounding. For example, we typically describe our age using only the whole number even if we are one month away from our birthdate. We don't round our age; we use a floor function. Let's do this with weight.

. generate weightf = floor(weight)

. list weight weight1 weight0 weightf in 1/5

weight weight1 weight0 weightf
1. 83.69 83.7 84 83
2. 83.69 83.7 84 83
3. 64.98 65 65 64
4. 81.65 81.7 82 81
5. 92.65 92.7 93 92

The third observation of weight is a good example. The value of 64.98 is very close to 65, but its floor value shown in weightf is 64.

The ceiling function is the opposite of the floor function. A ceiling function will return the next larger whole number.

. generate weightc = ceil(weight)

. list weight weight1 weight0 weightf weightc in 1/5

weight weight1 weight0 weightf weightc
1. 83.69 83.7 84 83 84
2. 83.69 83.7 84 83 84
3. 64.98 65 65 64 65
4. 81.65 81.7 82 81 82
5. 92.65 92.7 93 92 93

Now we can save our dataset.

. save mydata
file mydata.dta saved

You can watch a demonstration of these commands by clicking on the link to the YouTube video below. You can read more about these commands by clicking on the links to the Stata manual entries below.