如何在 SAS 9.4 中创建和使用 R 函数? [英] How to create and use an R function in SAS 9.4?

查看:52
本文介绍了如何在 SAS 9.4 中创建和使用 R 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R studio 中定义了一些 R 函数,这些函数有一些复杂的脚本和很多阅读行.我可以在 R studio 中成功运行它们.有没有什么办法,像宏一样把这些用户定义的函数转移到SAS 9.4来使用?我对 SAS 编程不太熟悉,因此最好将 R 函数复制到 SAS 中并直接使用.我想弄清楚如何进行转换.谢谢!

I have defined some R functions in R studio which has some complicated scripts and a lot of readlines. I can run them successfully in R studio. Is there any way, like macros to transfer these user-defined functions to SAS 9.4 to use? I am not pretty familiar with SAS programming so it is better just copy the R functions into SAS and use it directly. I am trying to figure out how to do the transformation. Thank you!

推荐答案

您无法在 SAS 中以本机方式运行 R 代码,而且您可能不想这样做.R 和 SAS 是完全不同的概念,SAS 更接近于数据库语言,而 R 是矩阵语言.在 SAS 中,高效的 R 方法很糟糕,反之亦然.(在 R 中尝试一个简单的循环,你会发现 SAS 的速度要快几个数量级;但在 R 中尝试矩阵代数).

You can't natively run R code in SAS, and you probably wouldn't want to. R and SAS are entirely different concepts, SAS being closer to a database language while R is a matrix language. Efficient R approaches are terrible in SAS, and vice versa. (Try a simple loop in R and you'll find SAS is orders of magnitude faster; but try matrix algebra in R instead).

不过,您可以在 SAS 中调用 R.您需要使用 SAS 的矩阵语言 PROC IML(可能是与您的 SAS 不同的许可证);到达那里后,您使用 提交/R 将代码提交给 R.您需要设置 RLANG 系统选项,并且您可能需要在 SAS 框上设置一些其他详细信息以确保它可以看到您的 R 安装,并且您需要 R3.0+.您还需要运行 SAS 9.22 或更高版本.

You can call R in SAS, though. You need to be in PROC IML, SAS's matrix language (which may be a separate license from your SAS); once there, you use submit / R to submit the code to R. You need the RLANG system option to be set, and you may need some additional details set up on your SAS box to make sure it can see your R installation, and you need R 3.0+. You also need to be running SAS 9.22 or newer.

如果您没有通过 IML 获得 R,您可以使用 xcall system,如果它们已启用并且您可以通过命令行访问 R.或者,您可以独立于 SAS 手动运行 R.无论哪种方式,您都可以使用 CSV 或类似的文件格式来回传输数据.

If you don't have R available through IML, you can use x or call system, if those are enabled and you have access to R through the command line. Alternately, you can run R by hand separately from SAS. Either way you would use a CSV or similar file format to transfer data back and forth.

最后,我建议您看看 SAS 中是否有更好的方法来解决您在 R 中解决的相同问题.通常有,而且通常非常快.

Finally, I recommend seeing if there's a better approach in SAS for the same problem you solved in R. There usually is, and it's often quite fast.

这篇关于如何在 SAS 9.4 中创建和使用 R 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆