Popular Post

_

Friday, January 24, 2014

R script to run in SAS: one more way to built IT-Control Chart

Sure SAS and SAS/GRAPH is good to plot (control) charts. See example here, But if you are a R lover you can also submit R script in SAS to do any analysis or charting you want. How?

SAS 9.3 can be used to run R scripts and packages within SAS/IML proc. To do that the following needs to be done:


1. Check if your SAS instance support R language. Just submit "proc options option=RLANG; run;" and check the sas log. If that is not supported, do next:
2. Add the following line at the end of the config file:
  C:\Program Files (x86)\SASHome\SASFoundation\9.3\nls\en\sasv9.cfg

-RLANG

3. Install  R-2.15.3 for Windows (32/64 bit) (note the R version 3 is not supported by SAS at this point)
4. Insert R-script to the following SAS/IML script:   
         proc iml;
          submit / R;
..... your R scrpt is here ....
        endsubmit;

5. Some simple test R-scripts samples to run in SAS is  below:
proc iml;
/* Comparison of matrix operations in IML and R */
print "----------  SAS/IML Results  -----------------";
x = 1:3;                                 /* vector of sequence 1,2,3 */
m = {1 2 3, 4 5 6, 7 8 9};               /* 3 x 3 matrix */
q = m * t(x);                            /* matrix multiplication */
print q;
print "-------------  R Results  --------------------";
submit / R;
  rx <- 1:3="" 1="" matrix="" nrow="1)            " of="" sequence="" span="" vector="">
  rm <- 1:9="" 3="" byrow="TRUE)" matrix="" nrow="3," span="" x="">
  rq <- matrix="" multiplication="" nbsp="" rm="" rx="" span="" t="">
  print(rq)
endsubmit;

More complex example:   Call R Packages from PROC IML

And finally my own script to build IT-Control Chart against CSV data is following:
proc iml;
submit / R;
## R script to plot IT-chart against CSV data - Igor Trubin 2009
cchrt <- appsdata.csv="" header="T," ocuments="" read.table="" sep="," sers="" span="" workshop="">
plot    (cchrt[,1],cchrt[,2],type="l",col="black",ylim=c(0,0.15),lwd=1.6,ann=F)
points (cchrt[,1],cchrt[,3],type="l",col="red",   ylim=c(0,0.15),lwd=1,ann=F)
points (cchrt[,1],cchrt[,4],type="l",col="green",ylim=c(0,0.15),lwd=1,ann=F)
points (cchrt[,1],cchrt[,5],type="l",col="blue", ylim=c(0,0.15),lwd=1,ann=F)
points (cchrt[,1],cchrt[,6],type="l",col="MAGENTA", ylim=c(0,0.1),lwd=1,ann=F)
mtext("I/O Time/Thrd",   side=2, line=3.0)
mtext("hours of week",   side=1, line=3.0)
mtext("IT-CHART",        side=3, line=1.0)
legend(10,0.16,c("Actual","UpperLimit","Mean","LowerLimit"),
          col=c("black","red","green","blue"),lwd=c(.2,.1,.1,.1),bty="n")
endsubmit;


The code is similar to the one I published in my other post here. And below is the result:

No comments:

Post a Comment