Showing posts with label file conversion. Show all posts
Showing posts with label file conversion. Show all posts

Tuesday, June 19, 2012

Batch Convert files from SPSS into Stata format

# Batch Convert files from SPSS into Stata format (using R)
# Written by Francis Smart [econometricsbysimulation.com]

# a. First install R (http://www.r-project.org/)

# b. Next install the package  foreign  from the Package Menu by selecting "Install Packages" [Packages>Install Package(s)>Select Closest Mirror>foreign].

# Now the following code should work:

# First load the foreign package
library(foreign)

# Set working directory (directory or super-directory (opposite of sub?) in which sav files can be found)
setwd("c:/*my_directory1*/*my_directory2*/")
# In R you must specify directories with either / or \\
# You must replace *my_directory* with the directory where to look for the sav files.

# Create a list of all of the sav files in the directory
dta_list<-dir(pattern = ".sav$", recursive=T, ignore.case = T)
# The default in this code is to look for files recursively (meaning within sub-directories).
# If you only want to look within the specific directory turn recursive to equal F.

# Loop through each member of the list
for(self in dta_list){

# Display what file will be read
  cat("Reading file ", self, "\n")

# Import the data as a dataframe (a class of objects in R intended for spreadsheet type data)
  data <- as.data.frame(read.spss(self, use.value.labels=TRUE, max.value.labels=Inf, to.data.frame=TRUE))

# Save the file substituting the sav extension for the dta extension
  write.dta(data, sub(".sav",".dta",self))
# dta files will be saved to the same directory as the orignal files
}

# I do not know how to correct the displayed error.  

# If you have any suggestions, please email me.

# Note this loop could easily be modified to accommodate many file formats since the "foreign" package  can handle many different types of file formats.

Sunday, June 17, 2012

3 Ways of Loading SPSS (sav) files into Stata

1. The easiest and most straightforward way is using the user written package usespss.

  This package however only works for 32 bit windows.

2. The next easiest method if you have it available is using StatTranfer which allows for the conversion of many types of files from one format to another:

  A license could be had for as little as $59 if you are a student.


3. The last option takes a few more steps but is free.

  a. First install R

  b. Next install the package Rcmdr from the Package Menu by selecting "Install Packages" [Packages>Install Package(s)>Select Closest Mirror>Rcmdr].

  c. Next open up the Rcmdr package by going to the menu [Packages>Load Package...>Rcmdr]

 d. When Rcmdr loads it should load the R Commander window.  Select from the menu [Data>Import Data>from SPSS data set]

 e. Name your data set Data1 [select 'ok'], now find your sav file.

 f. If there has been no problem up to this point you should now have a data set in memory called Data1.

 g. Now you can export to a dta file with the following command:
write.dta(Data1, "File Directory/File Name.dta")

(In R you need to specify directory dividers as either / or \\ not \.)