4

I want to export a dataset in the MASS package to SPSS for further investigation. I'm looking for the EuStockMarkets data set in the package.

As described in http://www.statmethods.net/input/exportingdata.html, I did:

library(foreign)
write.foreign(EuStockMarkets, "c:/mydata.txt", "c:/mydata.sps",   package="SPSS")

I got a text file but the sps file is not a valid SPSS file. I'm really looking for a way to export the dataset to something that a SPSS can open.

ABCD
  • 7,914
  • 9
  • 54
  • 90
  • 5
    Why not export as a regular csv? How is R inefficient in 'further investigation' of the data? :) – Roman Luštrik Aug 21 '14 at 07:29
  • That works as well. I thought I could just export to SPSS directly as stated in the link. Otherwise I'm ok with CSV. The book claimed it was possible so I tried it. – ABCD Aug 21 '14 at 07:31
  • Perhaps your SPSS version is not compatible with the type of file `foreign` produces? If SPSS is anything like Word or Excel, compatibility may go astray and crash into a tree, fast. – Roman Luštrik Aug 21 '14 at 07:32
  • Well, then I don't know which SPSS version to use for the file. – ABCD Aug 21 '14 at 07:33
  • Don't you probably want to get an `.sav` file to use it in SPSS? – Thomas Aug 21 '14 at 07:48
  • Yes, of course. So I thought the exported file is a .sav file just being renamed to something else. But I checked, it's not a sav file at all. – ABCD Aug 21 '14 at 07:51
  • 2
    Yes, `write.foreign` doesn't make `.sav` files. If you read the documentation, it writes the data in freeform text and then your `mydata.txt` file should be SPSS syntax to read that file into SPSS. For this reason, just loading the CSV directly into SPSS is probably just as easy. Alternatively, spit the .Rdata file out of R and use another tool like Stat/Transfer to do the file conversion if you want a `.sav`. – Thomas Aug 21 '14 at 08:04
  • Your comment should be an answer. If yo do it, i will accept it. – ABCD Aug 21 '14 at 11:03
  • From a programmer's view, it's much easier to use the CSV/SPS workaround than writing native SPSS format. Yet, `foreign:write.foreign()` has some serious issues, see http://stackoverflow.com/questions/40022111/export-r-data-frame-to-spss/40022112 – BurninLeo Oct 14 '16 at 11:35
  • R doesn't have very good support on SPSS – ABCD Oct 14 '16 at 11:45

4 Answers4

7

As Thomas has mentioned in the comments, write.foreign doesn't generate native SPSS datafiles (.sav). What it does generate is the data in a comma delimited format (the .txt file) and a basic syntax file for reading that data into SPSS (the .sps file). The EuStockMarkets data object class is multivariate time series (mts) so when it's exported the metadata is lost and the resulting .sps file, lacking variable names, throws an error when you try to run it in SPSS. To get around this you can export it as a data frame instead:

write.foreign(as.data.frame(EuStockMarkets), "c:/mydata.txt", "c:/mydata.sps", package="SPSS")

Now you just need to open mydata.sps as a syntax file (NOT as a datafile) in SPSS and run it to read in the datafile.

Ritchie Sacramento
  • 29,890
  • 4
  • 48
  • 56
2

Rather than exporting it, use the STATS GET R extension command. It will take a specified data frame from an R workspace/dataset and convert it into a Statistics dataset. You need the R Essentials for Statistics and the extension command, which are available via the SPSS Community site (www.ibm.com/developerworks/spssdevcentral)

JKP
  • 5,419
  • 13
  • 5
2

I'm not trying to answer a question that has been answered. I just think there is something else to complement for other users looking for this.

On your SPSS window, you just need to find the first line of code and edit it. It should be something like this: "file-name.txt"

You need to find the folder path where you're keeping your file: "C:\Users\DELL\Google Drive\Folder-With-Your-File"

Then you just need to add this path to your file's name: "C:\Users\DELL\Google Drive\Folder-With-Your-File\file-name.txt"

Otherwise SPSS will not recognize the .txt file.

Sorry if I'm repeating some information here, I just wanted to make it easier to understand.

1

I suppose that EuStockMarkets is a (labelled) data frame. This should work and even keep the variable and value labels:

require(sjlabelled)
write_spss(EuStockMarkets, "mydata.sav") 

Or you try rio:

rio::export(EuStockMarkets, "mydata.sav") 
Thomas Buhl
  • 303
  • 2
  • 7