forked from Bayer-Group/sas2r
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14-hybrid-programming.Rmd
68 lines (48 loc) · 2.22 KB
/
14-hybrid-programming.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Hybrid Programming with SAS and R
This section contains current solutions of hybrid programming.
## Use {sasr}
`{[sasr](https://github.com/insightsengineering/sasr)}` is a package built on
`{[reticulate](https://rstudio.github.io/reticulate/index.html)}` so that the Python package
`{[saspy](https://github.com/sassoftware/saspy)}`, an official python interface to connect SAS, can be called from R.
It adds some simple wrappers to the {saspy} functionalities to make the syntax similar to R.
Here are some simple examples of how you can use it.
```{r eval = FALSE}
library(sasr)
# Transfer data from R to SAS.
df2sd(mtcars)
# Execute SAS commands.
run_sas("
data test;
set work.mtcars;
run;
")
# Transfer data back to R.
sd2df("test")
```
This requires that you have a machine with SAS and it accessible through specific
[connection types](https://sassoftware.github.io/saspy/configuration.html),
where `ssh` is a common type of connection.
## Use {jupyter}/{quarto} and {saspy}
[jupyter](http://www.jupyter.org/) and [quarto](https://quarto.org/) are both capable of creating dynamic outputs from multiple languages.
Although the official SAS kernel is not supported, it can be installed through package [sas_kernel](https://github.com/sassoftware/sas_kernel) provided by [SAS software](https://github.com/sassoftware).
A quick example can be found at [Harvey Lieberman's blog](https://www.harveyl888.com/post/2022-06-27-quarto_sas_01/).
The requirement is the same as {sasr} as they both use {saspy} as the backend.
## Use {SASmarkdown}
{[SASmarkdown](https://github.com/Hemken/SASmarkdown)} is a package that installs the SAS engine to {knitr} so that in your `.Rmd` files
using SAS engines is possible.
However, this requires that SAS is installed locally on your machine, in contrast to client to a server.
## SAS with command line or ssh (not recommended)
As SAS can be called through command line, we can actually use the following to execute SAS codes
```{sh eval = FALSE}
sas script.sas
```
or, we can call SAS in R side
```{r eval = FALSE}
system("sas script.sas")
# using ssh
library(ssh)
ssh_connect()
scp_upload()
ssh_exec("sas script.sas")
```
Please note that using command line, you will need to have SAS installed on your machine.