rdatatablestatadta

Error in write_dta : A provided string value was longer than the available storage size of the specified column


I am trying to export my data table from R Studio to the dta format. I use write_dta function from haven library in R and get the following error:

A provided string value was longer than the available storage size of the specified column.

I am quite new to R and Stata and don't understand what it means and what should I do about it.


Solution

  • It sounds like you have a piece of long text in your data.frame. The write_dta has known issues handling long strings (https://github.com/tidyverse/haven/issues/437). You can trim the strings in your data.frame like this:

    df = as.data.frame(apply(YOUR_DATA, 2, function(x){
         if(class(x) == 'character') substr(x, 1, 128) else x}))
    

    And then try write_dta(df). The max length of 128 characters should be safe, but newer versions of Stata can handle a lot more.