%EXPORT_TO_R SAS Macro Code

May 6, 2011
By

(This article was first published on Econometric Sense, and kindly contributed to R-bloggers)

The SAS Analysis blog post 'A macro calls R in SAS for paneled 3d plotting' influenced my macro coding.   The following macro call:

%EXPORT_TO_R(DATA = YOURDATA) 

exports the SAS data set 'YOURDATA' as .csv and produces the R code for setting the R working directory and reading the file.

R-code written by %EXPORT_TO_R :

#  set R working directory
setwd("C:\\Documents and Settings\\wkuuser\\Desktop\\PROJECTS\\Stats Training")
 
# get data
dat.from.SAS <- read.csv("fromSAS_delete.CSV", header=T)
 
# check data dimensions
dim(dat.from.SAS)
names(dat.from.SAS)
 
Created by Pretty R at inside-R.org

Actual SAS code is below.

An application of this can be found here.

One potential drawback is that the file path for where you are exporting the data and the R code is hard coded in the macro. When I'm typically working in SAS and need this capability, I typically have a single 'development' folder for ad hoc support work in R. Ultimately when I've finished the program, I save the final R code in the specific project directory I'm working in.  So it's not a big deal once the macro is set up for the computer you are using.

There are many times where you may be in the SAS environment and require a specific statistical capability (like spatial regression ) that is in R or perhaps you don't have liscence for in SAS (like decision trees  with Enterprise Miner). Being able to quickly move into the R environment is nice. I've worked with submitting R code within the SAS IML environment, & it works great. However, its another system to start and run (outside of base SAS as I prefer the base SAS text editor over the IML environment). Updates to SAS PROC IML are supposed to deliver this capability within the base SAS environment, but I have not been able to get it to work. (I believe there are some timing issues with the SAS version and compatability with recent releases of R).

SAS code for macro:

/***********************************************************
   *  MACRO:      EXPORT_TO_R() - EXPORTS SAS DATA SET TO CSV
   *                            - SETS R WORKING DIRECTORY
   *                            - GENERATES R CODE TO READ DATA
   *  PARAMETERS: DATA   = SAS DATASET FOR EXPORTING
   *             
   *  NOTES: -YOU MUST PHYSICALLY SET THE FILE PATH TO THE DIRECTORY           
   *         -YOU WANT THE DATA TO BE EXPORTED TO
   *         -BECAUSE R IS CASE SENSITIVE, THIS MACRO IS CASE SENSITIVE
   *         
   ***********************************************************/

%MACRO EXPORT_TO_R(DATA =    );

  PROC EXPORT DATA = &DATA OUTFILE = "C:\DOCUMENTS AND SETTINGS\WKUUSER\DESKTOP\PROJECTS\STATS TRAINING\fromSAS_delete.CSV" REPLACE;
  RUN;
 
  PROC SQL;
    CREATE TABLE _TMP0 (STRING CHAR(110));
    INSERT INTO _TMP0
    SET STRING = '#  set R working directory'
       SET STRING = 'setwd("C:\\Documents and Settings\\wkuuser\\Desktop\\PROJECTS\\Stats Training")'
       SET STRING = '                           '
    SET STRING = '#  get data'
    SET STRING = 'dat.from.SAS <- read.csv("fromSAS_delete.CSV", header=T)'
    SET STRING = '           '
       SET STRING = '#  check data dimensions'
       SET STRING = 'dim(dat.from.SAS)'
       SET STRING = 'names(dat.from.SAS)';
    QUIT;
 
  DATA _NULL_;
    SET _TMP0;
    FILE "C:\DOCUMENTS AND SETTINGS\WKUUSER\DESKTOP\PROJECTS\STATS TRAINING\importFromSAS.TXT";
    PUT STRING;
  RUN;
%MEND;

To leave a comment for the author, please follow the link and comment on his blog: Econometric Sense.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , ,

Comments are closed.