BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
desireatem
Pyrite | Level 9

How do I translate this R code to SAS:

library(lme4)

sim<-function(m,n,b0=1,b1=-.5,b2=-1,testlow=1,testhi=10,xtest,xolftrue){

a0hat<-0

b0hat<-0

b1hat<-0

b2hat<-0

d0hat<-0

c0hat<-0

c1hat<-0

c2hat<-0

c3hat<-0

for(i in 1:m){

olf<-runif(n,1,10)

olftrue<-as.vector(t(matrix(olf,nrow=n,ncol=10,byrow=F)))

test<-sample(testlow:testhi,n,replace=T)

olftest<-ifelse(olf<test,test,0)

olfind<-ifelse(olf<test,1,0)

olft<-matrix(0,nrow=n,ncol=10,byrow=T)

olft[cbind(1:n,olftest)]<-1

olft<-t(apply(olft,1,cumsum))

olft<-as.vector(t(matrix(olft,nrow=n*10,ncol=1,byrow=T)))

x<-rnorm(n,3,1)

mx<-matrix(x,nrow=n*10,ncol=1,byrow=T)

x<-as.vector(mx)

alpha<-rnorm(n,0,.5)

malpha<-matrix(alpha,nrow=n*10,ncol=1,byrow=T)

alpha<-as.vector(malpha)

eps<-rnorm(n*10,0,1)

time<-matrix(1:10,nrow=n*10,ncol=1,byrow=T)

time<-as.vector(time)

time1<-pmin(time,olftrue)

time2<-pmax(time-olftrue,0)

y<-alpha+b0*x+b1*time1+b2*time2+eps

id<-as.vector(t(matrix(1:n,nrow=n,ncol=10,byrow=F)))

ot<-olft*time

try<-lmer(y~1+(1|id)+x+time1+time2)

a0hat<-a0hat+fixef(try)[[1]]

b0hat<-b0hat+fixef(try)[[2]]

b1hat<-b1hat+fixef(try)[[3]]

b2hat<-b2hat+fixef(try)[[4]]

timenew<-time-as.vector(t(matrix(olftest,nrow=n,ncol=10,byrow=F)))

olfind<-as.vector(t(matrix(olfind,nrow=n,ncol=10,byrow=F)))

try2<-lmer(y~1+(1|id)+x+timenew+olfind+timenew*olfind)

d0hat<-d0hat+fixef(try2)[[1]]

c0hat<-c0hat+fixef(try2)[[2]]

c1hat<-c1hat+fixef(try2)[[3]]

c2hat<-c2hat+fixef(try2)[[4]]

c3hat<-c3hat+fixef(try2)[[5]]

}

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

Here is a more recent version of the documenation: SAS/IML(R) 13.1 User's Guide

View solution in original post

4 REPLIES 4
LinusH
Tourmaline | Level 20

You probably have to learn IML programming, or hire one to do it for. There are some tools out there, but I doubt none can convert all R program to SAS out of the box.

Data never sleeps
lvm
Rhodochrosite | Level 12 lvm
Rhodochrosite | Level 12

There is no automated way. I doubt if any of our readers would want to spend a bunch of time doing this. You would first have to learn PROC IML and do it. There is another possibility: You can call R from within PROC IML. See the User's Guide, or check this out:

SAS/IML(R) 9.22 User's Guide

It is easy to embed R within an IML run, get the results and use with SAS.

Rick_SAS
SAS Super FREQ

Here is a more recent version of the documenation: SAS/IML(R) 13.1 User's Guide

Rick_SAS
SAS Super FREQ

Did the program editor eat some of your code? It looks like you are missing some closing brackets (the function and the for loop).

It looks like this is a simulation of a regression model. Presumably a mixed model, since it uses the lme4 package. Part of the missing code probably estimate parameters to obtain the sampling distribution.

You need to say how sophisticated your SAS programming skills are, and if you know SAS/IML or only the DATA step. It would also be nice if you tell us what you ultimately hope to accomplish.

There are two chapters about simulating regression models in the book Simulating Data with SAS, as well as many tips for efficient simulation There are also examples at this blog: Sampling and Simulation - The DO Loop  Here is a blog post that is similar to your example, but it is for a logistic model: Simulate many samples from a logistic regression model - The DO Loop

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 9369 views
  • 0 likes
  • 4 in conversation