Stata data files: Reading and writing to and from different file formats [DRAFT]

[You can skip straight to the examples if you prefer.]

 

Users of older versions of Stata will often want to read files created by newer versions of Stata. This can be difficult since, for example, Stata 12 cannot directly read data files created by Stata 13. Stata users will also often want to read data files that were created by some other program, e.g. SPSS, SAS, EXCEL. Conversely, using a Stata file in other software, such as SPSS, often requires  that the file be converted into a format that that software can read.

 

Note: Stata data file formats sometimes change when a new version of Stata is released, and sometimes do not. Sergiy Radyakin points out that, since Stata 8, and up through Stata 13.1, Stata has used 4 different file formats. The four versions are

 

Stata 8 - Stata 9.2

Stata 10.0 - Stata 11.2

Stata 12 - Stata 12.1

Stata 13 - Stata 13.1

 

Luckily, there are several programs, many of them free, that can make these tasks easier. Keep in mind that when converting between different formats some information may be lost or transformed (e.g. the new format may not be able to handle long strings; variable and value labels may get lost; variables may get renamed). For the programs listed below, read the documentation to find out about any known limitations or options you can use when converting. Keep in mind too that some of these programs are considered beta and hence may have problems that haven't been discovered yet.

 

Stat/Transfer (Commercial Software)- This may be the best option if you can afford it and/or can become Best Friends Forever with somebody who has a copy. Stat/Transfer reads and writes dozens of different file formats. Academic users, and students in particular, benefit from lower pricing. If you only need this program occasionally, harass your computing center to buy a copy that can be placed on a shared/public computer.

 

saveold (Official Stata Command) - saveold saves the dataset currently in memory in the format most recently previously used by Stata. If you have Stata 14, you can save in Stata 11, 12, or 13 format. If you are using Stata 13, the file will be saved in Stata 12 format. If you are using Stata 12 the file is saved in Stata 10.0 - 11.2 format. If using Stata 10 or 11 the file is saved in Stata 8 - Stata 9.2 format. This is especially useful if you have upgraded to the latest version of Stata while a person you are collaborating with is still using the previous version. It isn't enough, though, if you need to convert to an even older Stata version.

 

saveto9 (freeware, Sergiy Radyakin) - saveto9 converts dta files created by newer versions of Stata (up to version 12 as of this writing) into the format used by Stata 9 (which was also used by Stata 8). This is useful when sharing files with people who have really ancient versions of Stata. It is also useful if you want a file that can be read by SPSS (and perhaps other programs) since SPSS can only read Stata files written in the older format. saveto9 is a standalone program and does not require that Stata be installed. If you are using Stata 13, you can use saveold to save the file in Stata 12 format and then use saveto9 to convert to Stata 9 format.

 

savespss (freeware, Sergiy Radyakin) - savespss will export data from Stata's memory into an SPSS system datafile (*.sav).

 

usespss (freeware, Sergiy Radyakin) - USESPSS loads an SPSS-format (*.sav) dataset into memory. It supports compressed and non-compressed SPSS files from Windows and UNIX/Mac platforms. It preserves the variable and value labels, long variable names, extended missing values, and provides optimization of variable types. USESPSS is implemented as a plugin for 32-bit Stata for Windows (does not work in 64 bit).

 

use13 (freeware, Sergiy Radyakin) - use13 lets Stata 10-12 users read dta files created by Stata 13.

 

export (Official Stata Command) - export takes the Stata data in memory and exports it to one of several different formats. These include Excel, delimited (comma separated values (CSV), tab delimited), ODBC, Outfile (comma separated/ space separated/ fixed column format), sasxport, and xml. Many programs can read files created in one of these formats, so they may be a good choice if a direct conversion to the format you want is not possible. You will often lose a lot of information though, like value and variable labels.

 

import (Official Stata Command) - import lets Stata read data that is in one of several different formats. These include Excel, delimited (comma separated values (CSV), tab delimited), ODBC, Outfile (comma separated/ space separated/ fixed column format), sasxport, xml, and Haver Analytics database file.

 

read.dta and write.dta - If you are an R user, these programs read and write Stata files. I believe Stata 12 is the last supported version. I have never used R, so use at your own risk.

 


 

EXAMPLES.  Stat/Transfer is often the best solution or at least easiest solution for dealing with common problems; but if you don't have it here are some other alternativess.

 

Problem. A data file created in Stata 13 needs to be accessed by users with older versions of Stata.

 

Solution 1. Users of Stata 10-12 can use use13 to read the file.

Solution 2. Use saveold in Stata 13 to save the file in Stata 12 format.

Solution 3. If even older versions of Stata need to be able to read the file, then use saveto9 (after using saveold to get the file in Stata 12 format).

 

Problem. A Stata 13 (or earlier) data file needs to be read by SPSS.

 

Solution 1. Use savespss.

Solution 2. Use saveold (if using Stata 13) to save the file in Stata 12 format and then use  saveto9 . Just go straight to saveto9 if the file is in Stata 10-12 format.

 

Problem. Stata needs to read a file in a format not mentioned above.

 

Solution 1. Have somebody who has the program that created the file resave the file in a format that Stata can read.

Solution 2. If nobody has the necessary software you may need to break down and find a copy of Stat/Transfer .

 

Problem. A Stata file needs to be read by a program not mentioned above.

 

Solution 1. Find out what formats the program can read and then save a file in that format. Ideally the other software will be able to read at least an old version of Stata, or else SPSS sav files or SAS xport files. If worse comes to worst, most programs can read CSV (Comma Separated Values) files.

 

Solution 2. Again, quit your insane penny-pinching and get  Stat/Transfer. Your time is worth something too, and this is the kind of task that could take forever and the final solution still may not be optimal. Stat/Transfer, on the other hand, might be able to do it in less than a minute.

 


 

Please send any additions, corrections or other suggestions to Richard Williams.