使用 system() 命令在 R 中执行 SAS 程序 [英] Executing a SAS program in R using system() Command
问题描述
我的公司最近转换为 SAS 并且没有购买 SAS SHARE 许可证,因此我无法 ODBC 进入服务器.我不是 SAS 用户,但我正在编写一个需要从服务器查询数据的程序,我想让我的 R 脚本调用一个 .sas 程序来检索数据.我认为这是可能的使用
My company recently converted to SAS and did not buy the SAS SHARE license so I cannot ODBC into the server. I am not a SAS user, but I am writing a program that needs to query data from the server and I want to have my R script call a .sas program to retrieve the data. I think this is possible using
df <- system("sas -SYSIN path/to/sas/script.sas")
但我似乎无法让它工作.我在谷歌上花了几个小时,决定在这里问.
but I can't seem to make it work. I have spent all a few hours on the Googles and decided to ask here.
错误信息:
running command 'sas -SYSIN C:/Desktop/test.sas' had status 127
谢谢!
推荐答案
假设您的 sas 程序生成了一个 sas 数据集,您需要做两件事:
Assuming your sas program generates a sas dataset, you'll need to do two things:
通过
shell
或system
,让SAS运行程序,但首先cd
在包含sas可执行文件的目录中以防万一该目录不在您的 PATH 环境变量中.
Through
shell
orsystem
, make SAS run the program, but firstcd
in the directory containing the sas executable in case the directory isn't in your PATH environment variable.
setwd("c:\\Program Files\\SASHome 9.4\\SASFoundation\\9.4\\")
return.code <- shell("sas.exe -SYSIN c:\\temp\\myprogram.sas")
请注意,这返回的不是数据本身,而是操作系统发出的代码,它告诉您任务是否成功.代码 0
表示任务已成功.
Note that what this returns is NOT the data itself, but the code issued by the OS telling you if the task succeeded or not. A code 0
means task has succeeded.
在 sas 程序中,我所做的只是在 c:\temp
目录中创建 sashelp.baseball
的副本.
In the sas program, all I did was to create a copy of sashelp.baseball
in the c:\temp
directory.
使用为此编写的包之一将生成的数据集导入 R.Haven
是最新的,也是 IMO 最可靠的.
Import the generated dataset into R using one of the packages written for that. Haven
is the most recent and IMO most reliable one.
# 从 CRAN 安装 Haven:
install.packages("haven")
#导入数据集:
myData <- read_sas("c:\\temps\\baseball.sas7bdat")
你应该拥有它!
这篇关于使用 system() 命令在 R 中执行 SAS 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!