Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to write tagged NA in SAS format #755

Open
lz100 opened this issue May 17, 2024 · 0 comments
Open

Unable to write tagged NA in SAS format #755

lz100 opened this issue May 17, 2024 · 0 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@lz100
Copy link

lz100 commented May 17, 2024

When some values are tagged NA with labels, write_xpt throws errors

library(dplyr)

data_orig <- tibble(
  var =  c("1", "2", "2", "2", "3", "999", "1", "3", "777", NA),
)

data_labelled_stata_sas <- data_orig |> 
  mutate(
    var = case_when(
      var == 999 ~ haven::tagged_na("d"),
      var == 777 ~ haven::tagged_na("r"),
      .default = as.double(var)
    ) |>
      labelled::set_value_labels(
        c(
          yes   = 1,
          maybe = 2,
          no    = 3,
          "don't know" = haven::tagged_na("d"),
          "refused"    = haven::tagged_na("r")
        )
      ) |> 
      labelled::set_variable_labels(
        "var" = "Do you like ice cream?"
      )
  )

haven::write_xpt(data_labelled_stata_sas, "data_labelled_sas.xpt")
#> Error: Failed to insert value [6, 1]: A provided tag value was outside the range of allowed values in the 
#> specified file format.

Is there a way to make it work?

@gorcha gorcha added the bug an unexpected problem or unintended behavior label May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants