Export file . SAP in R

Asked

Viewed 72 times

2

It would be possible to export a file in . SAP using R or Python ?

Find a tutorial that said that in R to export to . SAP it would be as follows:

write_sas(df, "table.sas7bdat")

But I was left with a doubt, when I do this extraction the end of the file is really with the extension ". sas7bat", only on another system that exports in . SAP it gets the extension . SAP

  • 2

    The extension is the one you put in the name of the file you are creating. Change "table.sas7bdat" for "table.sap will solve.

  • then I can just change the end of the extension that’s right?

  • It would not be a file. SAS?

  • 1

    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.

  • @Rafaelbarros then the extension I need is . SAP

1 answer

2

The package 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.

  • then my doubt is: the extension I need is . SAP and not SAS, will it interfere ?

  • Save a file through SAS and find the default extension used. It will make a difference just for you. SAS will be able to read the file regardless of its decision

  • So, what happens I’m going to send this file to a person who uses the . SAP, SAS does not enter at any time in the list of these files, so my doubts, I really need the file to be . SAP and not SAS

  • Are you sure this is a ". SAP" file? I just found this extension related to music

Browser other questions tagged

You are not signed in. Login or sign up in order to post.