Importing and exporting data files

We often find data on the Web in formats other than Stata, or we need to share data with our collaborators who (unfortunately) are not Stata users.

There are several ways to get data into and out of Stata. While this list is probably not exhaustive, it covers most of the formats that we encounter at CPC. Some methods are available from within Stata, while others require shareware or commercial software.

Stata commands

import and export

These are native Stata commands to read and write several data file formats. The commands import excel and export excel read and write Excel spreadsheets (.xls and .xlsx). The command export excel is quite flexible, allowing you to write to a specific cell in a specific sheet in an Excel workbook if you need to. The commands import sasxport and export sasxport read and write SAS xport (transport) format files and will even read and write value labels from/to a SAS formats.xpf file. See help import and help export for details. See Exporting Stata Results to MS Office for an example of export excel. Here's a simple example of importing an Excel spreadsheet:

import excel "C:\tables\workbook.xlsx", firstrow

usespss

This command, written by Sergiy Radyakin, is available from the SSC archive. It handles Windows SPSS data files with the .sav suffix (called "system" files in SPSS). It does not handle SPSS portable files with the .por suffix. See usesas and Stat/Transfer below for software that can deal with SPSS portable files.

outsheet and insheet

The outsheet command is a way to write the variable names and values (but no labels) into tab-delimited text files, comma-separated values (.csv) files, or files with other delimiters. Most software will read a text file with one of these delimiters between the values. Stata also reads these text files with the insheet command. So, you can export data from something like MySQL to a .csv file and read it into Stata with insheet.

xmluse and xmlsave

These commands transfer data between Stata and MS Excel's xml format. (For most purposes you'll probably find the import excel and export excel commands more useful.) The xml format is a portable text version of Excel's .xls or .xlsx format files. xmlsave produces a file that Excel reads directly. In order to use an Excel file in Stata, though, you need to open it in Excel and save it as type xml. There are two xml "Save As" formats in Excel. The "XML spreadsheet 2003 (*.xml)" option seems to work better.

 

Other software

usesas and savasas

While these are implemented as Stata commands, they are listed here under "Other software" because they both require you to have SAS installed locally as well as Stata. At CPC we have both of these commands installed and they work wonderfully.

If you have a standalone computer with both SAS and Stata installed, you may want to download these commands from the SSC archive. You may need to edit the code to tell Stata where to find the SAS command, but that is all explained in the help and in comments inside the code.

Stat/Transfer

This is not intended as an advertisement for a commercial product, but it really is useful. It converts data files between statistical software, spreadsheets, databases, etc. with ease. If you find yourself needing to import or export data across software formats often, it is worth the investment. It is distributed by Stata Corp as well as by the developer Circle Systems. At CPC, send an email to CPC Help for access to Stat/Transfer.

use13, saveold

At CPC we keep Stata up to date with the most recent version. Currently, that is version 14. You may be working with colleagues who have an earlier version that cannot read version 14 files. When they try to use one of your files, they will get the message that the file is not in Stata format.

One option is Stata's saveold command. You can use it to save your file in version 13, 12, or 11 format, and send this older version of the file to your colleagues. The command is:

     saveold path/filename.dta, version(nn)

If you're still using version 13, you can ask your colleagues with older versions to download the command use13, written by Sergiy Radyakin at the World Bank and available from the SSC archives.

 

Review Again?

 

Another topic?


Wink Plone Theme by Quintagroup © 2013.

Personal tools
This is themeComment for Wink theme