Skip to content

Instantly share code, notes, and snippets.

@benjamin-chan
Last active October 12, 2015 17:01
Show Gist options
  • Save benjamin-chan/73019d049763cff1d448 to your computer and use it in GitHub Desktop.
Save benjamin-chan/73019d049763cff1d448 to your computer and use it in GitHub Desktop.
R script to call SAS
callSAS <- function (f, path=tempdir(), workDir=sprintf("E:\\Share\\Temp\\%s\\SASWork", Sys.info()["user"])) {
# Usage:
# > callSAS("getClaimsMed.sas")
if (grepl(".sas$", f)) {
exeFile <- "C:\\Program Files\\SASHome\\SASFoundation\\9.3\\sas.exe"
sasFile <- sprintf("%s\\%s", path, f)
logFile <- sprintf("%s\\%s", path, gsub(".sas$", ".log", f))
cmd <- sprintf("\"%s\" -sysin \"%s\" -log \"%s\" -print \"%s\" -work \"%s\"",
exeFile,
sasFile,
logFile,
logFile,
workDir)
system(cmd, invisible=FALSE)
cat(readLines(sasFile), sep="\n")
if (length(grep("^ERROR:", readLines(logFile)) > 0)) {
warning(sprintf("\nERROR: See line %s in %s.", grep("^ERROR:", readLines(logFile)), logFile))
} else {
message(sprintf("No errors\nSee %s for details.", logFile))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment