importspss.ado (requires R)

June 29, 2010 at 3:01 pm 6 comments

| Gabriel |

Mike Gruszczynski has a post up pointing out that you can use R to translate files, for instance from SPSS to Stata. I like this a lot because it let’s you avoid using SPSS but I’d like it even better if it let you avoid using R as well.

As such I rewrote the script to work entirely from Stata. Mike wanted to do this in Bash but couldn’t figure out how to pass arguments from the shell to R. Frankly, I don’t know how to do this either which is why my solution is to have Stata write and execute an R source file so all the argument passing occurs within Stata. This follows my general philosophy of doing a lot of code mise en place in a user-friendly language so I can spend as little time as necessary in R. (Note that you could just as easily write this in Bash, but I figured this way you can a) make it cross-platform and b) attach it to “use” for a one-stop shop “import” command).

*importspss.ado
*by GHR 6/29/2010
*this script uses R to translate SPSS to Stata
*it takes as arguments the SPSS file and Stata file
*adapted from http://mikegruz.tumblr.com/post/704966440/convert-spss-to-stata-without-stat-transfer 

*DEPENDENCY: R and library(foreign) 
*if R exists but is not in PATH, change the reference to "R" in line 27 to be the specific location

capture program drop importspss
program define importspss
	set more off
	local spssfile `1'
	if "`2'"=="" {
		local statafile "`spssfile'.dta"
	}
	else {
		local statafile `2'	
	}
	local sourcefile=round(runiform()*1000)
	capture file close rsource
	file open rsource using `sourcefile'.R, write text replace
	file write rsource "library(foreign)" _n
	file write rsource `"data <- read.spss("`spssfile'", to.data.frame=TRUE)"' _n
	file write rsource `"write.dta(data, file="`statafile'")"' _n
	file close rsource
	shell R --vanilla <`sourcefile'.R
	erase `sourcefile'.R
	use `statafile', clear
end

Entry filed under: Uncategorized. Tags: , , , .

People don’t think in sigmas? An approximate rant

6 Comments

  • 1. Peter L  |  June 29, 2010 at 4:56 pm

    Useful. But I get grumpy when your code doesn’t end with have a nice day…

  • 2. Martin Weiss  |  June 30, 2010 at 6:45 am

    Are you guys aware of -usespss- on the ssc archive? As I understand it, it lets you access SPSS files from within Stata, without any calls to external software.

    • 3. gabrielrossman  |  June 30, 2010 at 10:05 am

      “usespss” says it’s Windows only

  • 4. Mike Gruszczynski  |  June 30, 2010 at 11:13 am

    Fantastic, thanks much for this! Always enjoy the blog.

  • 5. Importnew.ado (requires R) « Code and Culture  |  October 10, 2011 at 11:15 am

    […] who are still on Stata 10 that they were having trouble opening Stata 12 .dta files, I rewrote my importspss.ado script to translate Stata files into an older format, by default Stata […]

  • 6. Scraping Using twitteR (updated) « Code and Culture  |  December 20, 2011 at 11:04 pm

    […] more robust but this requires passing arguments to R. I’d previously solved this problem by having Stata write an entire R script, which Stata understood as having variables (or “macros”) but which from R’s […]



%d bloggers like this: