There is a package foreign
with a function write.foreign()
that can write a SPS and CSV file. The SPS file than can read the CSV fiel into SPSS including labels. Fine so far, but there are some issues with that function:
- Newer SPSS versions may show an error that you have too few format definitions in
DATA LIST
- If there are "labels" for numeric variables stored via
attr()
, these are lost. - Even if the SPSS vesion supports strings up to 32767, the function
write.foreign()
stops if there are more than 255 in any variable. - Theres a star (*) if any character variables are used, but newer SPSS versions cannot handle that.
- The CSV file is comma-separated and does (can) not use quotes, therefore no commas are allowed in strings (character)
- Non-ASCII caracters (e.g. umlauts) will crash the import
- Should you have a character that contains any NA value, you'll see...
... an error message like this:
Error in if (any(lengths > 255L)) stop("Cannot handle character variables longer than 255") :
missing value where TRUE/FALSE needed
I spent a lot of time with that and then found a good posting (http://r.789695.n4.nabble.com/SPSS-export-in-R-package-foreign-td921491.html) to start on and make it better. Here's my result, I'd like to share with you.
The SPSS extension command STATS GET R can read a data frame directly into an SPSS dataset from a saved R workspace. If this extension command is not already installed (it will show up on the File menu), it can be installed from the Utilities menu (Statistics 22-23) or the Extensions menu (Statistics 24+).
To export an R data.frame to SPSS, use
write_sav
from the haven package:This function is a replacement for
foreign:write.foreign
to handle the issues stated above.Note: To avoid issues with SPSS finding the CSV file, please specify the full path (!) at least for
datafile
(also if using the originalforeign:write.foreign()
).Note: This script will replace a tabulator (TAB) and other spacing (incl. CR+LF) in strings by a blank without warning. One may consider using
GET DATA
instead of the troublesomeDATA LIST
to solve that limitation.Note: There may be a warning
In FUN(X[[i]], ...) : probable complete loss of accuracy in modulus
- this refers to counting the decimals and can be ignored.Note:
POSIXlt
andPOSIXct
variables are not yet handled by the script properly.On the long term, the changes might be considered to be merged into the
foreign
package. Unfortunately, the bug reporting system for the r-project is currently limited to previously registered developers.