Bug 16086 - write.foreign(...package='SPSS') incorrect SPSS syntax being generated in foreign_0.8-61
Summary: write.foreign(...package='SPSS') incorrect SPSS syntax being generated in for...
Status: CLOSED FIXED
Alias: None
Product: R
Classification: Unclassified
Component: I/O (show other bugs)
Version: 3.4.0
Hardware: All All
: P5 normal
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2014-11-25 00:17 UTC by Jeremy Jackson
Modified: 2017-04-30 07:13 UTC (History)
1 user (show)

See Also:


Attachments
Fixed writeForeignSPSS.R (3.76 KB, text/x-matlab)
2014-11-25 00:17 UTC, Jeremy Jackson
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jeremy Jackson 2014-11-25 00:17:03 UTC
Created attachment 1692 [details]
Fixed writeForeignSPSS.R

Fix for this issue in writeForeignCode.R from the foreign package has been submitted as an attachment.  See lines 53-58.

Found that foreign package is generating incorrect SPSS syntax codefile when specifying formats in the DATA LIST command.

Per page 575 of IBM_SPSS_Statistics_Command_Syntax_Reference.pdf (ftp://public.dhe.ibm.com/software/analytics/spss/documentation/statistics/20.0/en/client/Manuals/IBM_SPSS_Statistics_Command_Syntax_Reference.pdf):
"For freefield data, variables with no specified formats take the default F8.2 format. However, an asterisk (*) must be used to indicate where the default format stops. Otherwise, the program tries to apply the next specified format to every variable before it and issues an error message if the number of formats specified is less than the number of variables."

Since writeForeignSPSS() applies a format to every character field, every character field should also have an asterisk.  Thus, write.foreign(...package='SPSS') should take the following data.frame:

df <- data.frame(w=4:6, x=letters[1:3], y=1:3, z=LETTERS[1:3], stringsAsFactors=F)

And use it to produce the following DATA LIST:

DATA LIST file="~/documents/myFile.sav" free (",")
/ w  * x (A1) y  * z (A1)  .

Currently, the code produces this DATA LIST, which causes SPSS to assign the (A1) format (alphanumeric of length 1) to column y, which should get the default format (F8.2) (8 digit number with two decimal places):

DATA LIST file="~/documents/myFile.sav" free (",")
/ w  * x (A1) y  z (A1)  .
Comment 1 Uwe Ligges 2017-04-30 07:13:50 UTC
This is is rather more complicated, as starred variables must not follow each other and there must not be a star for the very first variable.
In any case, fixed in the svn version.