read xlsx file into R and export in to SAS7bdat format

lib(openxlsx)

mydata=read.xlsx("C:/Users/vivek.v/Desktop/R_prog/xxxxfilenamexxxxxx20Apr2020.xlsx", sheet = "owssvr" , startRow = 1, colNames = TRUE, detectDates = TRUE)

above code helped me to create R format output

i want to export this data into sas7bdat

im completely new to R studio.can any one help me to create this .

https://cran.r-project.org/web/packages/rio/vignettes/rio.html

i have given all the packges still im not able to

....where do you get stuck ?

install.packages("rio") #only need this the first time
library(rio) 
install_formats() #only need this the first time

export(mtcars, "mtcars.sas7bdat")

lib(openxlsx)
mydata=read.xlsx("C:/Users/vivek.v/Desktop/R_prog/PDXXXXXXX20Apr2020.xlsx", sheet = "owssvr" , startRow = 1, colNames = TRUE, detectDates = TRUE)
export(mydata, "mydata.sas7bdat")

log console says

export(mydata, "mydata.sas7bdat")
Error in write_sas_(data, normalizePath(path, mustWork = FALSE)) :
Writing failure: A provided name contains an illegal character.

i have install rio also

SAS is very strict about it's column names.
You should names(mydata) to check that they are SAS compliant and change if necessary

there are (.-) in variable names how i can modify those

there are ways to do it manuallly with names() and also with tidyverse packages rename()
but theres also a neat package that does well for many needs with very automated approach.

mybaddf <- structure(list(c(5.1, 4.9, 4.7, 4.6, 5, 5.4), c(
  3.5, 3, 3.2,  3.1, 3.6, 3.9), c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7), c(
  0.2, 0.2,  0.2, 0.2, 0.2, 0.4)), .Names = c(
  "terrible name",
  "?also bad", "234dontlike", NA_character_
), row.names = c(NA, -5L), class = c(
  "tbl_df",
  "tbl", "data.frame"
))

 mybaddf
# A tibble: 5 x 4
# `terrible name` `?also bad` `234dontlike`    NA
# <dbl>       <dbl>         <dbl> <dbl>
# 1             5.1         3.5           1.4   0.2
# 2             4.9         3             1.4   0.2
# 3             4.7         3.2           1.3   0.2
# 4             4.6         3.1           1.5   0.2
# 5             5           3.6           1.4   0.2


library(janitor)
(better_df <- janitor::clean_names(mybaddf))
 # A tibble: 5 x 4
 # terrible_name also_bad x234dontlike    na
 # <dbl>    <dbl>        <dbl> <dbl>
 # 1           5.1      3.5          1.4   0.2
 # 2           4.9      3            1.4   0.2
 # 3           4.7      3.2          1.3   0.2
 # 4           4.6      3.1          1.5   0.2
 # 5           5        3.6          1.4   0.2

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.