私はあなたの問題は、SASは、あなたが何の書き込みアクセス権を持っていないディレクトリにログファイルを書き込もうとすることだと思います。あなたはSASにそのログファイルの別の場所(との.outファイル)を渡すことができます。
sas_log <- tempfile()
sas_out <- tempfile()
cmd <- sprintf(
'sas.exe -nosplash -icon -sysin "%s" -log "%s" -print "%s"',
sas_script, sas_log, sas_out
)
return_code <- system(cmd) # Runs sas and saves the return code to
私はこれがあなたの問題を解決すべきだと思います。おそらく、あなたのログファイルは、SASスクリプトが生成した結果と同じ場所にあり、あいまいな一時ディレクトリではないでしょう。
高度な例:
私はかつて、ロギングおよびエラー処理が含まれてSASスクリプトのラッパー関数を書きました。多分これはあなたに便利ですが、あなたはそれがあなたのシステム上で実行するために、それを少し変更する必要があります:
library(futile.logger)
library(purrr)
library(assertthat)
library(stringi)
library(magrittr)
sasget <- function(
infile,
outfile = NULL,
sas_script,
sas_path = "C:/Program Files/SAS/x86/SASFoundation/9.4",
sas_config = "C:/Program Files/SAS/x86/SASFoundation/9.4/nls/de/sasv9.cfg"
){
# Precondtions
assert_that(purrr::is_scalar_character(infile))
assert_that(is.null(outfile) || purrr::is_scalar_character(outfile))
assert_that(file.exists(sas_script))
assert_that(dir.exists(sas_path))
assert_that(file.exists(sas_config))
# Process arguments
if(is.null(outfile)) outfile <- tempfile()
sas_log <- paste0(outfile, '.log')
sas_out <- paste0(outfile, '.lst')
# Launch sas job
owd <- getwd()
setwd(sas_path)
on.exit(setwd(owd), add = TRUE)
cmd <- sprintf(
'sas.exe -nosplash -icon -sysin "%s" -set filein "%s" -set fileout "%s" -log "%s" -print "%s"',
sas_script, infile, outfile, sas_log, sas_out)
flog.debug('Launching SAS job')
flog.trace(cmd)
return_code <- shell(cmd) # Run SAS and retain return code
# Process SAS Log
log <- suppressWarnings(try(readLines(sas_log), silent = TRUE))
errors <- log %>%
stringi::stri_subset_regex('^ERROR.*:')
lapply(log, flog.trace)
# Postconditions
ok <- TRUE
if(!identical(return_code, 0L)){
flog.error('SAS process returned nonzero return code: %s', return_code)
ok <- FALSE
}
if(isTRUE(file.size(outfile) == 0L)){
flog.error('SAS process returned empty file')
ok <- FALSE
}
if(length(errors) > 0L){
lapply(errors, flog.error)
ok <- FALSE
}
if(ok){
flog.debug('SAS file transfer successful')
} else {
flog.fatal('There were Errors, please check SAS log: %s', sas_log) %>%
stop()
}
# output
res <- outfile
return(res)
}
注:
- 私はに特異的であったいくつかのコードチャンクを削除する必要がありました
- マクロ変数をSASスクリプトに渡す方法の例として、パラメータ
infile
とoutfile
を残しておきました。これは、私が取り組んでいたプロジェクトです。あなたは、SASスクリプトで次のように使用することができます:%let fileIn = %sysget(filein)
。
コードはコマンドライン、つまりSASコードを実行するシステムコマンドから機能しましたか? – Reeza
'system'と' system2'の代わりに 'processx'をチェックアウトしたいかもしれません –