2.4 Entering data using the Data Editor
Stata has a Data Editor, shown in figure 2.2, in which we can enter our data from the coding sheet in table 2.2. The Data Editor provides an interface that is similar to that of a spreadsheet, but it has some features that are particularly suited to creating a Stata dataset.
Figure 2.2. The Data Editor
Before we begin, let’s type the commandclearin the Command window. This step will give us a fresh Data Editor in which to enter data. To open the Data Editor in edit mode, typeedit in the Command window. Alternatively, click on the icon on the toolbar that looks like a spreadsheet with a pencil pointing to it (see figure 2.3) or click onData⊲Data Editor⊲Data Editor (Edit). The Data Editor in edit mode is a convenient way to enter or change data, but if you just want to see the data, you should use the Data Editor in browse mode. The icon for the Data Editor (Browse) is just to the right of the icon for the Data Editor (Edit); it is a spreadsheet with a magnifying glass on top (see figure 2.3). You can also type the commandbrowsein the Command window.
30 Chapter 2 Entering data
Figure 2.3. Data Editor (Edit) and Data Editor (Browse) icons on the toolbar Data are entered in the white columns, just as in other spreadsheets. Let’s enter the data for the first respondent from the coding sheet in table 2.2. In the first cell under the first column, enter the identification number, which is a1for the first observation.
Press the Tab key to move one cell to the right, and enter the value for the person’s gender, which is a2for the first observation. Keep entering the values and pressingTab until you have entered all the values for the first participant. Now you can press the Enterkey, which will move the cursor to the second row of the Data Editor (Edit), and then press the Home key, which will move the cursor to the first column of this row.
You can proceed to enter the rest of the data. TheTabkey is smart, so after you enter the last value for the second row of data, pressingTabwill move the cursor to the first column of the third row. However, before doing this it would be good to enter more informative variable names to replace the default names ofvar1,var2, etc. To change a variable’s name, click anywhere in the column for that variable or click on its name in the Variables pane at the top right of the Data Editor, for example,var1. Then in the Properties pane at the lower right of the Data Editor, changeNamefromvar1 to id. The name can be up to 32 characters, but it is usually best to keep the name short.
Stata is case sensitive, so it is best to use lowercase names. By using lowercase names, you will not have to remember what case to use because the names will all be lowercase.
From the Properties pane, we can also change the variable’s label to be “Respondent’s identification” by typing this text in theLabel box. Our Data Editor (Edit) will look like figure 2.4.
2.4 Entering data using the Data Editor 31
Figure 2.4. Variable name and variable label
Once we click outside the changed field in the Properties pane or press Enter, we will see the new variable name displayed. Change each of the remaining generic variable names to rename and label them as listed in table 2.3.
Table 2.3. New variable names and labels Generic variable name New variable name Variable label
var2 gender Participant’s gender
var3 education Years of education
var4 sch st Ratings of schools in your state
var5 sch com Ratings of schools in your
community of origin
var6 prison Ratings of prison sentences
var7 conserv Conservatism/liberalism
As well as allowing us to specify the variable name and variable label, the Properties pane includes a place to specify the format of the variable. The current format is%9.0g, which is the default format for numeric data in the Data Editor. This format instructs Stata to try to print numbers within nine columns. Stata will adjust the format if you enter a number wider than nine columns.
32 Chapter 2 Entering data I typed the letter l for the number1
A common mistake is to type a letter instead of a numeric value, such as typing the letterlrather than the number1or simply pressing the wrong key. When this happens for the first value you enter for a variable, Stata will make the variable a “string” variable, meaning that it is no longer a numeric variable. When this happens for a subsequent value you enter for a variable, Stata will display an error message. If you made the mistake of inputting a variable as a string variable instead of a numeric variable, you need to correct this. Simply changing the letter to a number will not work because a string variable may include numbers, such as a variable for a street address. Stata will still think it is a string variable. There is a simple command to force Stata to change the variable from a string variable back to a numeric variable: destring varlist, replace. For example, to change the variableidfrom string to numeric, you would typedestring id, replace.
If you have string data (that is, data that contain letters or symbols), you will see a format that looks like %9s, which indicates a variable that is a string (the s) with nine characters. For more detailed information about formats, see the Stata Data- Management Reference Manual, which is available under Help ⊲ PDF Documentation.
Some people like to enter string data where they enter yes or no, male or female, and so on. It is usually best here to simply enter numbers; I will show how to add value labels for the numbers in the next section. Entering numbers will benefit you later because some Stata commands work fine with string variables, but many of them require numeric values. After entering the rest of your data, the Data Editor (Edit) should now look like figure 2.5.