The package Haven provides two ways to export file to the SAS
in the R
. These two shapes are packaged in functions write_sas()
and write_xpt()
. You can read in their documentation which form is best suited to your needs.
Any file you generate with these functions can be read in SAS, INDEPENDENT OF ITS NAME (AND THEREFORE EXTENSION)
The SAS
will be able to read files with the wrong extension (as long as it is in fact a file to be read by SAS
).
library(haven)
td <- tempdir()
# Arquivo com extensao errada
write_sas(mtcars, paste0(td, "/iris.xlsx"))
read_sas(paste0(td, "/iris.xlsx"))
#> # A tibble: 32 x 11
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
#> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
#> 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
#> 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
#> 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
#> # ... with 22 more rows
The SAS
will also be able to read files without extension:
# Arquivo sem extensao
write_sas(mtcars, paste0(td, "/sem_extensao"))
read_sas(paste0(td, "/sem_extensao"))
#> # A tibble: 32 x 11
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
#> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
#> 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
#> 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
#> 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
#> # ... with 22 more rows
And even to the right extent:
# O mesmo serve para as funções *_xpt
write_xpt(mtcars, paste0(td, "/mtcars.xpt"))
read_xpt(paste0(td, "/mtcars.xpt"))
#> # A tibble: 32 x 11
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
#> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
#> 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
#> 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
#> 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
#> # ... with 22 more rows
Created on 2019-02-22 by the reprex package (v0.2.1)
Extension note
The file extension helps to understand the format of that file, but it only helps and is designed basically for humans.
Programs often use other methods to check how to read that file. One of them, I don’t know if the only or even the most used, is to have a magic extension number. More information about magic number can be found here.
In windows the file extension is as the operating system decides which program will use to open the file by default, but you can choose another in hand if you want or he doesn’t know which to use.
The extension is the one you put in the name of the file you are creating. Change
"table.sas7bdat"
for"table.sap
will solve.– Tomás Barcellos
then I can just change the end of the extension that’s right?
– Brenda Xavier
It would not be a file. SAS?
– Rafael Barros
There may be a bug in this package, please see the comments on this issue: https://stackoverflow.com/questions/52031042/r-export-to-sas-but-cannot-open.
– leogregianin
@Rafaelbarros then the extension I need is . SAP
– Brenda Xavier