0

I'm trying to write an SPSS file with some haven_labelled variables and factors that are created from that variable. It's just convenient for me and my use case to use nearly identical variable names. I've used all lower case for the haven_labelled variables and title case for the respective factor variable.

When I export the data frame with write_sav, the SPSS records the variable name of the title case factor with var1, rather than the title case, in this case Francophone. Note that when I change the name of the variable significantly, it prints the variable name.

#This makes the data frame of haven labelled variable and a corresponding factor
test<-structure(list(francophone = structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), labels = c(Francophone = 1, `Not Francophone` = 0), label = "Dichotomous variable, R is francophone", class = c("haven_labelled", "vctrs_vctr", "double")), Francophone = structure(c(1L, 1L, 1L,1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Not Francophone", "Francophone"), label = "Dichotomous variable, R is francophone", class = "factor")), row.names = c(NA,-10L), class = c("tbl_df", "tbl", "data.frame"))

#Create a second factor variable equivalent to the first factor, but with a different variable name
test$Franco<-test$Francophone
library(tidyverse)
library(haven)
#Write out the file; sorry I do not know how to use tmpfile() in this case. 
test %>% 
  write_sav(., path="~/Desktop/test2.sav")

Image of SPSS File

spindoctor
  • 1,719
  • 1
  • 18
  • 42
  • 1
    And what is your question? You might want to familiarize yourself with the requirements of variable naming in SPSS, i.e. you can't have identical variable names, where identical in SPSS also means taht upper/lowercase is ignored. – deschen Sep 24 '21 at 20:30
  • I thought I posed the question in the title. The question is how do I write to an sav file that are different only in case. But I have learned here and elsewhere that SPSS treats variables case insensitively so this is impossible. Thanks.! – spindoctor Sep 25 '21 at 23:11

1 Answers1

0

To close the loop, variable names in SPSS have to be unique: https://www.ibm.com/docs/en/spss-statistics/version-missing?topic=list-variable-names

This has always been the case (and will probably not change).

deschen
  • 10,012
  • 3
  • 27
  • 50