Using Stata for Quantitative Analysis. Kyle C. Longest
male or a female.
It is a good practice to always save a copy of your data files and only work with the duplicated version. When working with and analyzing data, you often will be forced to change aspects of the data files. For example, you may need to create a new variable or change something about an existing variable. But it is important to have an original version of the data, just in case something undesired occurs. Don’t worry too much; most alterations you perform can be undone or recovered. Working with a duplicate copy of the data is simply an added protection.
To save a duplicate copy of the data file you have just opened, open the File menu and click on Save As. You can then enter a new file name, such as Chapter 1 Data mycopy.dta, and click Save. This is the procedure you will use whenever you want to save a new version of your data file.
Data Browser and Editor
If this is the first time you are working with data, it may be helpful to actually “see” the data. Even if you have experience using data, it may often be helpful to look at the data you are examining. To see the data file in Stata, you can click on the Data Browser icon,
FIGURE 1.3 • DATA BROWSER WINDOW
As was noted in the Preface, the vast majority of Stata features and commands are similar across versions (e.g., Stata 15, 14, 13, etc.). This is true of Stata data files by and large. All Stata data files that are created and/or saved in an older version can be read by a newer version (i.e., they are forward compatible). This means that if you are using Stata 15 but are working with colleagues who are using Stata 14, any files they send to you will open without a problem.
During certain upgrades, however, Stata data files cease to be “backward” compatible, meaning files saved in a newer version cannot be opened by older versions. Stata 13 happened to be one of those upgrades. If you are using Stata 15 and send a data set that you saved in Stata 15 to your colleagues who are using Stata 13 (or earlier), they will not be able to open it. (Note: This is not a problem if you are moving files between Stata 15 and Stata 14, as these two versions are completely compatible with each other.)
Do not despair. Stata has built in a very simple feature to overcome this problem. If you know that you want the data you are using in Stata 15 to be opened by older versions, you need to take one extra step (from the process just explained).
First, click on the File menu and then click on Save As. Now, use the drop-down menu in the Save as Type box and select the Stata 13 Data (*.dta) option or the Stata 12/11 Data (*.dta) (if your colleague happens to be using a version prior to Stata 13). Note that you do not need to change the file extension; it is still .dta. Once you have named your file, click Save. You will know that you have saved the data correctly when the output in the Results window starts with .saveold, which is telling you that the file has been saved in a way that makes it readable by the previous versions. Again, note that when you save a file in this way, it can still be used in Stata 15.
This new window, as is denoted in its upper left-hand corner, is the Data Editor (Browse) window. The “(Browse)” aspect indicates that you are only looking at the data, not actually changing them.
In this window, you see all five of the variables that were listed in the Variables window. As was mentioned earlier, each row is a different case (i.e., a National Study of Youth and Religion [NSYR] respondent), and each column is a different variable. Each cell then contains information on the given variable for that case. For example, the case in the first row is a “Male” respondent who mentioned that “Mormon” was his other religion. To close this window, click on the red “X” in the upper right-hand corner.
There may be times when you would want to change the value of a particular case on an individual variable. One way to do so is by using the Data Editor window. (A more efficient way to change the values of multiple cases is covered in The 5 Essential Commands: replace (if) section of Chapter 2.) To begin, click on the Data Editor icon,
After you have opened the Data Editor window, use the direction keys (or mouse) to highlight the cell you would like to change. For example, you may have realized that the first case’s age was incorrectly entered in the data file. Instead of being 23 years old, this case should only be 22 years old. To make this change, once you have the cell in the first row listed under agecats highlighted, type 22 and press Enter. This case’s value for the variable agecats has now changed. When you close the Data Editor window, this operation has been recorded and displayed in both the Review and Results windows.
You may have noticed that when you changed the first case’s value using the Data Editor window, the following text was displayed in the Results window:
replace agecats = 22 in 1
(1 real change made)
Whenever you use the menus or a point-and-click method for performing an operation in Stata, it displays in the Results and Review window the command that would be entered in the Command window to perform the same operation. In this Data Editor example, you can see that the command to change a value is -replace-. If you had entered this full command into the Command window and pressed Enter, the same change would have been made. At times, it may be helpful to perform an operation for the first time using the menus, but, as will be discussed in much more detail in Chapter 2, it is extremely beneficial to know and use the commands via the Command window for the majority of the operations you need to perform.
The rest of this book will discuss how to perform operations using the Command window. But to see the connection between the menu-based operation and the Command window, try this: Type (or copy and paste) the full command (except the first “.”) that was displayed in the Results window when you closed the Data Editor window into the Command window. Now change the “22” to “23.” The command should read
replace agecats = 23 in 1
Then press Enter. Open the Data Browser window again and notice the change to the first case’s value under agecats.
Entering Your Own Data
Many data files you analyze will already be in Stata format or in a format that can be easily converted to Stata format (more on this topic below). Yet there may be times when you need to enter the data from a study. For example, if you distributed a survey through the mail, you will need to input the responses to each question for each case so that you can analyze them in Stata.
The first step in entering your own data after you have opened Stata is to open the Data