Читать книгу Using Stata for Quantitative Analysis - Kyle C. Longest - Страница 18

Using Different Types of Data Files in Stata

Оглавление

Some data files may not be available in Stata format. Therefore, a few steps are needed to work with these files in Stata. It would be virtually impossible to cover every possible data file type and how each can be transferred to be usable in Stata. Instead, the most common types will be covered. Also note that there are other computer software programs that are specifically designed to convert data files into various formats (e.g., Stat/Transfer). If you have access to such a program, it is probably the most effective and efficient way to transfer files into a Stata format. Some statistical software packages also offer the option of saving a data file in a different format, which often includes the Stata.dta extension.

One of the most frequently encountered data file types that is not Stata-ready is a Microsoft Excel file. Usually, these files are denoted with the.xls extension, but other extensions (e.g., .csv) that are generated or readable by Microsoft Excel can be treated in a similar fashion.

While there are a few different ways to transfer data saved in this format, Stata offers a straightforward method for bringing data from an Excel file into Stata that does not even require Excel. After opening Stata, click on the File menu, followed by Import. Select the Excel spreadsheet (*.xls *.xlsx) option,4 and the window as shown in Figure 1.4 will appear.

4You will also notice that you could select several different data file formats from this window. The general procedure for each of these formats is very similar to the procedures discussed for Excel files but may contain particular steps for specific files.


FIGURE 1.4 • IMPORT DATA FROM EXCEL FILE WINDOW

Click on the Browse button to find the Excel data that you would like to turn into a Stata data set. Once you have selected the Excel file, you can pick a particular worksheet from that file or even a particular set of cells by using the corresponding boxes. Once you have selected a file, you need to make one key determination that is based on how the data are entered in the Excel file. Notice the radio box to denote “Import first row as variable names.” Whether you should select this option will depend on whether your Excel file contains variable names in the first row or whether it contains only data. The two formats are shown in Figures 1.5 and 1.6.


FIGURE 1.5 • FIRST ROW AS VARIABLE NAMES


FIGURE 1.6 • FIRST ROW AS DATA

If the first row contains variable names (as shown in Figure 1.5), click the radio button next to Import first row as variable names (when you do this, notice that the data shown in the preview window will change). Then click OK. If the first row contains data (as shown in Figure 1.6), leave this radio button unchecked and click OK. After you have selected the option that fits with the type of data file you have, close the Data Editor and follow the previously described steps to save the data from within Stata as a Stata data file. Once you have saved your data as a Stata data file, you can simply open and use this version of your data.

The Import is the most effective option to bring in data from other formats. There is another option to bring in Excel data, but it requires that you have access to and some familiarity with Microsoft Excel and is not as efficient as the Import procedure. To start, open the data file in Microsoft Excel. Then highlight the entire worksheet that contains the data and copy it (either by right clicking and choosing Copy or using the copy function Ctrl+C). Next, in Stata, open the Data Editor window, highlight the upper left data cell, right click and choose Paste, or use the paste function (Ctrl+V). Once you have pasted in the data, you should be presented with a window that asks whether you want to Treat First Row as Data or Treat First Row as Variable Names. At this point you will need to determine whether the Excel data are entered with the first row as data (Figure 1.6) or variable names (Figure 1.5) to make the appropriate selection. After you have selected the option that fits with the type of data file you have, close the Data Editor and follow the previously described steps to save the data from within Stata as a Stata data file.4

4This “copy and paste” method may seem to be the easiest way to transfer data from Microsoft Excel into a Stata format, especially for novice users. But there are some disadvantages to this strategy. More practiced users should transform Excel worksheets into .csv files and then implement the -insheet- command. The specifics of this command are beyond the scope of this introductory text, but the Stata Help Files section of Chapter 8 provides information on how Stata’s Help files can be used to learn how to use this command.

Using Stata for Quantitative Analysis

Подняться наверх