Wednesday, April 27, 2011

A macro calls R in SAS for paneled 3d plotting

SAS and R could complement each other. SAS is a versatile ETL (extraction, transformation and loading) machine and its statistical procedures based on generalized linear model are impeccable. R would bring cutting-edge data mining and data visualization technologies at low cost (or no cost). Although the two packages dwell in distinctive ecosystems (for example: different OS/ETL/database/reporting layers) [Ref. 1], mixed programming by combining them together would make an analytics shop invincible.

Some SAS programmers like to use SAS/IML to call R’s functions [Ref. 2]. However, it seems that SAS/IML fails to work with the latest versions of R since 2.12 [Ref. 3]. Others tend to play tricks to call R into SAS’s data step to meet their daily needs [Ref. 4]. In this post, the macro below would call the ‘lattice’ package of R in SAS, on a PC platform, to draw paneled three dimension images, since currently SAS’s SG procedures don’t own such an option. The good thing is that there is no need to check the version of R installed before running it. And the modification of this macro can be extended to other applications to call R in SAS.

1. ‘Keep an Eye on the emerging Open-Source Analytics Stack’. Revolution R Blog.
2. Zhengping Ma. ‘Data mining in SAS with open source software’. SAS Global 2011.
3. ‘SAS/IML incompatible with latest releases of R’. SAS-L. 11APR2011.
4. Liang Xie. ‘Regularized Discriminant Analysis’.

/*******************READ ME*********************************************
* VERSION:     SAS 9.2(ts2m0), windows 64bit
* DATE:        25apr2011
****************END OF READ ME*********************j********************/

****************(1) MODULE-BUILDING STEP******************;
%macro scatter3dpanel(data = , x = , y = , z = , factor = , 
                      width = , height = , outfile = );
   *  MACRO:      scatter3dpanel()
   *  PARAMETERS: data   = dataset for plotting
   *              x      = x-axis variable
   *              y      = y-axis variable
   *              z      = z-axis variable
   *              factor = partition factor variable
   *              width  = width of output graph
   *              height = height of output graph
   *              outfile= location of output image
  proc export data = &data outfile = "d:\tmp.csv" replace;
  proc sql;
    create table _tmp0 (string char(80));
    insert into _tmp0 
    set string = 'tmp=read.csv("d:/tmp.csv", header=T)'
    set string = 'attach(tmp)'
    set string = 'library(lattice)'
    set string = 'windows()'
    set string = 'cloud(sas_zvar~sas_xvar+sas_yvar|as.factor(sas_factor), pretty=T)'
    set string = 'dev.print(device=png, width=sas_width, height=sas_height, file="sas_file")';
  data _tmp1;
    set _tmp0;
    string = tranwrd(string, "sas_xvar", propcase("&x"));
    string = tranwrd(string, "sas_yvar", propcase("&y"));
    string = tranwrd(string, "sas_zvar", propcase("&z"));
    string = tranwrd(string, "sas_factor", propcase("&factor"));
    string = tranwrd(string, "sas_width", "&width");
    string = tranwrd(string, "sas_height", "&height");
    string = tranwrd(string, "sas_file", translate("&outfile", "/", "\"));

  data _null_;
    set _tmp1;
    file "d:\callRinSAS.r";
    put string;

  options noxsync noxwait;
  x ' "d:\Program Files\R\R-2.12.1\bin\R.exe" CMD BATCH --vanilla --slave "d:\callRinSAS.r" ';

****************(2) TESTING STEP******************;
%scatter3dpanel(data =, x = length, y = wheelbase, z = horsepower, 
            factor = type, width = 1200, height = 600, outfile = d:\test1.png );

****************END OF ALL CODING***************************************;


  1. ------Rick Wicklin (

    Great post, as usual. Other “references” that deserve mention are macros by Phil Rack and Phil Holland. They also use the “X” command to call R as an external process.

    One advantage to using the SAS/IML interface to R is that it is easier to transfer data back and forth between SAS and R
    (data sets <==> data frames and SAS/IML matrices <==> R matrices). You can also call R in a loop from within a SAS/IML program, which is handy for BY group processing or for applying an analysis to a slew of data sets/matrices/parameters, and so forth. For more on calling R from SAS/IML software, see

    Incidentally, I also dedicate a chapter of my book ( to calling R from SAS and I show a number of cool ways to use the best features of each. It was a fun chapter to write!

    With respect to SAS being unable to call R 2.12, that 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.

    -------Rick Wicklin (

  2. Thanks very much for your large information .And knowledge full description . I think it is Sus a topic that many kinds of people face many problems. thanks for this. Information visualization Low

  3. Website wonderful I wish I could be like my website this site , My talking about things that are also important , such as your website,
    The first part is talking about my website is the sale of
    chef knives
    and the most important brands offered for sale such as
    global knife set review

    global knives reviews

    zwilling ja henckels knives reviews

    henckels knives reviews

    victorinox chef knives reviews

    victorinox kitchen knives reviews

    chef knives reviews

    The second is talking about the website, the problems of water leaks and how its solutions and areas that are out of our branches is
    افضل شركة
    كشف تسربات المياه

    كشف تسربات المياه بالرياض

    كشف تسربات المياه بجدة

    كشف تسربات المياه بالدمام

    كشف تسربات المياه بدون تكسير

    كشف تسربات المياه بالمدينة المنورة