Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing a SAS program in R using system() Command

Tags:

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.

error message:

running command 'sas -SYSIN  C:/Desktop/test.sas' had status 127 

Thanks!

like image 894
Ben Avatar asked Jul 02 '26 09:07

Ben


1 Answers

Assuming your sas program generates a sas dataset, you'll need to do two things:

  1. Through shellor system, make SAS run the program, but first cd 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")

    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.

    In the sas program, all I did was to create a copy of sashelp.baseball in the c:\temp directory.

  2. Import the generated dataset into R using one of the packages written for that. Haven is the most recent and IMO most reliable one.

    # Install Haven from CRAN:
    install.packages("haven")
    # Import the dataset:
    myData <- read_sas("c:\\temps\\baseball.sas7bdat")

And there you should have it!

like image 129
Dominic Comtois Avatar answered Jul 03 '26 21:07

Dominic Comtois



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!