[R] Please help with write.foreign(SPSS)

peter dalgaard pdalgd at gmail.com
Tue Dec 7 11:00:20 CET 2010


On Dec 7, 2010, at 09:19 , Jim Lemon wrote:

> On 12/07/2010 09:45 AM, He wrote:
>> Dear all,
>> 
>> I get stuck when i try to export the data into SPSS format/file using
>> "write.foreign()"
>> Do you know how to do it exactly?
>> 
>> What i have done is
>> 1) First i type the following code in R:
>> 
>> df<-data.frame(id,year,res1)
>> names(df)<-c("idcode","year","resarrvl")
>> write.foreign(df,"z:/daten/res.dat","z:/daten/res.sps",package="SPSS")
>> 
>> 2) Then I run "res.sps" in SPSS
>> 
>> DATA LIST FILE= "k:/res.dat"  free (",")
>> / idcode year resarrvl  .
>> 
>> VARIABLE LABELS
>> idcode "idcode"
>>  year "year"
>>  resarrvl "resarrvl"
>>  .
>> 
>> VALUE LABELS
>> /
>> idcode
>> 1 "000CF7CC"
>>  2 "000D0212"
>>  3 "000D055F"
>> ......
>> 
>> 1339 "06996BC5"
>>  1340 "069972BF"
>> .
>> 
>> EXECUTE.
>> 
>> 3) After running this file, there automatically appears a SPSS file.
>> The variable name is correct, however, there is no data in that file.
>> The character variable id has different "values" like 1.00 =
>> "000CF7CC", where 000CF7CC should be in the tab of data view instead
>> of variable view.
>> 
>> 4) Try to open "res.dat"
>> It comes out the window "Text import wizard"
>> The first column which should be the vairable id, shows only the
>> "values" of id, i.e. 1, 2 and so on. And no real variables names.
>> 
> Hi He,
> Your data list statement looks like it is trying to read in a non-SPSS file (maybe a CSV file). Are you sure you want to do this?
> 
> Jim

That makes OK sense to me, the object is to _write_ an spss file in the end. write.foreign writes a plain text .dat file plus an spss "syntax file" which supposedly should read the file and generate the spss file. 

What looks odd to me is that res.dat appears to be written t z:/daten/ but read from k:,
so I wonder whether spss is actually finding the same file?

It is not particularly odd that a direct import of res.dat comes out without variable names and value labels, because that information is contained in res.sps.

A closer look at k:res.dat (as a plain text file) and any messages from running res.sps could be illuminating.

-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list