Introduction
Sometimes, working with a dataset requires you to create rounded versions of a variable. In this blog, we’ll show you how to use Stata to round numbers.
Creating a Dataset
Let’s create some mock data in Stata that we can use for rounding. Let’s generate weight data in kilograms. Try entering the following code into Stata:
set obs 400
drawnorm weight_a, mean(80) sd(10)
Here’s what you get:
We can now show you how to round these data.
Rounding
It’s a good idea to leave your original variable—the one to be rounded—alone and to generate new rounded versions of that variable.
Try the following commands:
gen weight_integer = round(weight_a)
gen weight_50 = round(weight_a, 0.50)
gen weight_25 = round(weight_a, 0.25)
gen weight_10 = round(weight_a, 0.10)
Here, you’ve created four new variables. The first one is an integer; the next three are rounded to 0.50, 0.25, and 0.10, respectively. All you have to do, when you are rounding in this way, is to insert a comma and tell Stata where you are rounding to. Here’s what you have now:
BridgeText can help you with all of your statistical analysis needs.