run sas from r using shell function -
i using r execute sas program following dominic comtois's approach (executing sas program in r using system() command). when executed test codes, error massage showed
error: insufficient authorization access c:\program files\sashome\x86\sasfoundation\9.3\test_yl.log. test sas program was: libname xx 'f:/sitetools'; data xx.test; input species $ bec_i_c $ agetype age height; cards; fd c 1 35 14.3 fd c 0 35 14.3 fd 1 35 14.3 fd 0 35 14.3 pl 1 65 14.3 pl 1 25 14.3 pl 0 65 14.3 pl 0 25 14.3 ; run;
how resolve issue. highly appreciated.
yong
i think problem sas tries write logfile directory have no write access. can pass alternate location log file (and .out file) sas:
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 , saves return code
i think should solve problem. want log file in same location whatever result sas script produces, , not in obscure temporary directory.
advanced example:
i once wrote wrapper function sas script includes logging , error handling. maybe useful you, have modify bit make run on system:
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 , 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 errors, please check sas log: %s', sas_log) %>% stop() } # output res <- outfile return(res) }
notes:
- i had remove code chunks specific project working on, hope didn't leave confusing in.
- i left parameters
infile
,outfile
in example of how pass macro variables sas script. can use them in sas script this:%let filein = %sysget(filein)
.
Comments
Post a Comment