[R] Problem exporting data using write.foreign

Andrew Miles rstuff.miles at gmail.com
Wed Oct 20 19:59:21 CEST 2010


I was able to figure this one out.  It turns out that truncation was  
not the problem, as I had no variables with names longer than 32  
characters (that is quite a long name!).  I document my process here  
so that future users with the same problem can benefit.

First, I examined the code for the function throwing the error:

foreign:::make.SAS.names

This revealed that the error message I was getting was thrown any time  
the transformed/reformatted variable name was too long or duplicated  
another variable name:

     if (any(nchar(x) > nmax) || any(duplicated(x)))
         stop("Cannot uniquely abbreviate the variable names to ",
             nmax, " or fewer characters")

I was able to isolate which variables were the problem as follows:

which(duplicated(x))

where x is the vector of transformed variable names which were  
transformed using make.SAS.names code:

     x <- sub("^([0-9])", "_\\1", varnames)
     x <- gsub("[^a-zA-Z0-9_]", "_", x)
     x <- abbreviate(x, minlength = nmax)

This allowed to discover that I had two variables, retire_sp and  
retire.sp, which became the identical "retire_sp" after transformation.

Simple, really, but frustrating until you get into the code and see  
what is happening, and how to isolate the offending variables.

Andrew Miles


On Oct 20, 2010, at 1:18 PM, Nordlund, Dan (DSHS/RDA) wrote:

>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
>> project.org] On Behalf Of Andrew Miles
>> Sent: Wednesday, October 20, 2010 10:10 AM
>> To: r-help at r-project.org
>> Subject: [R] Problem exporting data using write.foreign
>>
>> My question is about the write.foreign() command in the foreign
>> package.  I use a command like the following to try and output data
>> and a code file to read my data into SAS.
>>
>> write.foreign(data.frame.object, datafile="filepath",
>> codefile="filepath", package="SAS", dataname="myData")
>>
>> With my data set, it gives the following error:
>>
>> Error in make.SAS.names(names(df), validvarname = validvarname) :
>>   Cannot uniquely abbreviate the variable names to 32 or fewer
>> characters
>>
>> I tried to write reproducible code but could not.  I'm not sure where
>> to go from here.  What are the naming protocols for variables so that
>> they can be exported using write.foreign()?
>>
>> Thanks!
>>
>> Andrew Miles
>>
>
> Well, the error message tells you that the names must be unique when  
> truncated to 32 characters.  Apparently, you have at least 2  
> variables that have the same name when truncated to 32 characters.
>
> Hope this is helpful,
>
> Dan
>
> Daniel J. Nordlund
> Washington State Department of Social and Health Services
> Planning, Performance, and Accountability
> Research and Data Analysis Division
> Olympia, WA 98504-5204
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list