Friday, May 6, 2011

%EXPORT_TO_R SAS Macro Code

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;

3 comments:

  1. Matt,
    Thanks for this post. I can't tell if you read my comments to Charlie's post (http://www.sasanalysis.com/2011/04/macro-calls-r-in-sas-for-paneled-3d.html) but it sounds like you are using R 2.12 with SAS 9.22 (also known as SAS 9.2M3) and SAS is having trouble finding/starting R. If so, this is a temporary situation caused because R changed its directory structure between 2.11 and 2.12. SAS 9.22 (which shipped before R 2.12 was released) is looking for certain DLLs in places where they no longer exist. This is corrected in SAS 9.3.

    A workaround is to install R 2.11; then PROC IML will work as expected.

    If what I've outlined (R=2.12, SAS=9.22) is not correct, then contact SAS technical support, since we are not aware of any other issues.
    Best wishes,
    Rick

    ReplyDelete
  2. Thanks. My version of R actually is currently 2.12.1 & my version of SAS is 9.2 (level 2M3). I think I did read your previous comments before. I may try installing an older version of R on my other computer with SAS and see what happens.

    ReplyDelete
  3. Thanks for give a right suggestion for all the user.About for A heuristic guide to econometrics, statistics, applied analytics, biometrics, data mining, machine learning, experimental design

    ReplyDelete