Data are often stored in a way that is accurate but difficult to read. Dates are a special case and we will discuss them later. But numbers are often stored with many decimal places of accuracy, and we would like to make them easier to view while retaining that accuracy. We can do this by customizing the display format of those variables.
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 |
The variable label for weight tells us that it is measured in kilograms. Let's list the first 10 observations in the variable weight.
. list weight in 1/10
weight | |
1. | 83.69 |
2. | 83.69 |
3. | 64.98 |
4. | 81.65 |
5. | 92.65 |
6. | 68.95 |
7. | 54.55 |
8. | 66.57 |
9. | 102.4 |
10. | 75.52 |
Weight is measured with two digits to the right of the decimal. But we don't want to see those decimal values when viewing weight, so let's change the display format to show only the whole numbers.
. format %6.0f weight
Display formats begin with the % character, which is followed by the number of characters to display. The digit after the decimal specifies the number of digits to display to the right of the decimal, which, in this example, is 0. Let's list the data for weight again.
. list weight in 1/10
weight | |
1. | 84 |
2. | 84 |
3. | 65 |
4. | 82 |
5. | 93 |
6. | 69 |
7. | 55 |
8. | 67 |
9. | 102 |
10. | 76 |
We don't see the digits to the right of the decimal anymore. The digits are still in the dataset, and we haven't lost any information. We have simply changed the way the data are displayed.
You can do many things with format, and you can learn more by typing help format.
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.
Watch Data management: How to change the display format of a variable.
Read more in the Stata Data Management Reference Manual; see [D] describe, [D] format, and [D] save.