[R] Translation from R codes to SAS.
xlr82sas
xlr82sas at aol.com
Wed Dec 2 07:37:52 CET 2009
ychu066 wrote:
>
> my teachers doesnt understand R and I don't know how to use SAS.
> Anyone interested in translating my codes to test whether your SAS codes
> are as good as R???
> I can test it on SAS codes once you have translated it ....
>
>
>
> regards:working:
>
It is quite easy to convert R to SAS IML. Here is an example of R/S+ code
and SAS IML code. It is not exactly the same problem but is very colse.
I find IML has much of the same functiionality of R, but R blows SAS away
with the 2000+ packages out there.
R/S+ CODE
a=c(2,2,1,0,1,0,1,5,1,1,0,2,2,2,2,1,1,2,3,0,0,0,1,1,0,2,1,1,1,1,0,1,0,1,1,1,1,0,1,1,15,27,0,0,0,0,0,0)
b=c(355,389,773,213,231,43,120,105,381,283,294,561,276,416,393,202,103,210,135,196,122,175,55,38,561,114,147,230,88,167,116,1171,706,203,287,253,313,162,441,393,2620,1429,101,232,70,25,196,676)
c=c(0,1,1,1,0,1,0,2,0,0,1,0,1,0,1,1,2,0,1,0,1,1,0,0,2,3,0,0,0,0,0,0,0,2,0,0,0,0,0,0,9,41,0,0,0,0,0,0)
d=c(176,206,184,108,116,46,124,112,384,135,301,142,278,212,197,105,97,107,138,96,119,172,58,38,274,108,143,242,88,172,61,377,325,183,280,272,154,160,112,124,2625,2854,51,115,75,24,195,225)
n <- a+b+c+d
Rr <- (c+d)/(a+b)
k.T <- 1/(Rr+1)
k.C <- Rr/(Rr+1)
OR.i <- ((a+k.T)*(d+k.C))/((b+k.T)*(c+k.C))
varlnOR.i <- 1/(a+k.T)+1/(b+k.T)+1/(c+k.C)+1/(d+k.C)
w.i <- 1/varlnOR.i
plogOR.i <- sum(w.i*log(OR.i))/sum(w.i)
pvarlogOR.i <- 1/sum(w.i)
pOR.iv.i <- exp(plogOR.i)
l.pOR.iv.i <- exp(plogOR.i-qnorm(.975)*sqrt(pvarlogOR.i))
u.pOR.iv.i <- exp(plogOR.i+qnorm(.975)*sqrt(pvarlogOR.i))
SAS CODE
Just to read a bunch of columns rom s SAS dataset(dataframe in R)
use inv1st(keep=Key) ;read all var _num_ into keyxpo;
use inv1st(keep=TrtEvn) ;read all var _num_ into axpo;
use inv1st(keep=TrtNonEvn);read all var _num_ into bxpo;
use inv1st(keep=CtlEvn) ;read all var _num_ into cxpo;
use inv1st(keep=CtlNonEvn);read all var _num_ into dxpo;
/* Vertical to horizontal */
a=axpo`; * The apostrophe means transpose;
b=bxpo`;
c=cxpo`;
d=dxpo`;
key=keyxpo`;
Rr=(c+d)/(a+b); * ratio of control vs trtment group sizes;
k_T=1/(Rr+1);
k_C=Rr/(Rr+1);
OR_C_R = ((a+k_T)#(d+k_C))/((b+k_T)#(c+k_C));
VAR_OR_C_R = 1/(a+k_T) + 1/(b+k_T) + 1/(c+k_C) + 1/(d+k_C);
W_R = 1/VAR_OR_C_R;
LOG_OR_IV_R = sum(W_R#log(OR_C_R))/sum(W_R);
VAR_LOG_OR_IV_R = 1/sum(W_R);
OR_IV_R = exp(LOG_OR_IV_R);
OR_IV_UP_R = exp(LOG_OR_IV_R + probit(.975)#sqrt(VAR_LOG_OR_IV_R));
OR_IV_LOW_R = exp(LOG_OR_IV_R - probit(.975)#sqrt(VAR_LOG_OR_IV_R));
CC_TA=shape({0},1,3,0);
CC_TA= OR_IV_R||OR_IV_LOW_R||OR_IV_UP_R;
create CC_TA from CC_TA;append from CC_TA;
--
View this message in context: http://n4.nabble.com/Translation-from-R-codes-to-SAS-tp819110p932816.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list