BookmarkSubscribeRSS Feed
bibastat
Calcite | Level 5

i need help in certain program i am writing in a do until loop, loop doesn't end unless condition is broken from the first time, if the condition continues to hold the program does not end, Could u pls help me ?

proc iml;

n=3;

p=2;

m=10;

k=1;

nruns=100;

yvec=0;

sigma=1;

T=n*(k+m);

x1={1,4,3};

x2={2,3,5};

xp={1 2,4 3,3 5};

l=j(n,1,1);

x=l||x1||x2;

x11=repeat(x1,(m+k));

x22=repeat(x2,(m+k));

i=j(T,1,1);

xx=i||x11||x22;

z=T({ [30] 0 [3] 1});

z0=z#i;

zx1=z#x11;

zx2=z#x22;

xxf=i||z0||x11||zx1||x22||zx2;

beta0=1;

beta1=2;

beta2=3;

alpha=0.005;

alpha1=1-(1-alpha)**2/3;

df1=p+1;

df2=T-2*(p+1);

fdis=finv((1-alpha),df1,df2);

do i=1 to m;

epslon1=sigma* normal(repeat(-1,n));

y=beta0+(beta1)*x1+(beta2)*x2+epslon1;

yvec=yvec//y;

xty=T(x)*y;

beta=inv(T(x)*x)*xty;

yhat=x*beta;

msej=ssq(y-yhat)/(n-p);

msec=msec//msej;

end;

yvec1=yvec;

mse=sum(msec)/m;

shiftvec={0,0.05,1,1.5,2,2.5,3,3.5,4,4.5,5};

nshf=nrow(shiftvec);

arlvec=0;

do k=1 to nshf;

shift=shiftvec[k,];

c=0;

do i=1 to nruns;

arl=0;

ftest=0;

do until (ftest>fdis);

epslon=sigma* normal(repeat(-1,n));

y=beta0+(beta1+shift)*x1+(beta2)*x2+epslon;

yvec1=yvec1//y;

bhatr=inv(T(xx)*xx)*T(xx)*yvec1[2:34,];

yhatr=xx*bhatr;

err=yvec1[2:34,]-yhatr;

sser=T(err)*err;

bhatf=inv(T(xxf)*xxf)*T(xxf)*yvec1[2:34,];

yhatf=xxf*bhatf;

erf=yvec1[2:34,]-yhatf;

ssef=T(erf)*erf;

msef=T(erf)*erf/(T-2*(p+1));

ftest=(sser-ssef)/((p+1)*msef);

arl=arl+1;

end;

c=c+arl;

end;

avrl=c/nruns;

avrlvec=avrlvec//avrl;

end;

print sser ssef c arl avrl arlvec ftest fdis msef shift;

quit;

10 REPLIES 10
Rick_SAS
SAS Super FREQ

All the computations inside the DO-UNTIL loop are the same after the second iteration. Therefore the loop will never end.

You write

yvec1=yvec1//y;

which makes it look like you are changing yvec1. However, the rest of the loop only uses the first 34 rows, which are constant.

While you are debugging the program, use

DO UNTIL(ftest>fdis | arl>5);

in order to avoid the infinite loop.

bibastat
Calcite | Level 5

First Thanx for ur help. Now I want to solve it without specifying the arl because the goal is to compute arl at different shifts. I know yvec1 is the same but y should be different each loop, am i right or wrong? Could you suggest any other way to solve it without specifying the arl. Thanx in advance

Rick_SAS
SAS Super FREQ

1) First, get the code working for a particular choice of arl. When it is all debugged and working, you can add a loop for different shifts.

2) Yes, y is different for each loop, but you never access y inside the DO-UNTIL loop.  You append each y to the END of yvec1, but you only use the BEGINNING of yvec1.

3) I don't know what you mean by "any other way to solve it without specifying the arl" because I don't understand what the program is doing. Perhaps you should annotate the code with comments and explanations. I see a simulation of random errors for a linear regression models with various choices for beta1, but I don't understand what you are trying to accomplish, nor do I understand why you expect the DO-UNTIL loop to end.  Why should the value of the F test ever exceed the critical value that you set?

bibastat
Calcite | Level 5

Ok really thanks because now i am starting to know what the problem is, ok let me try to explain what i need and if u could guide me what to do if it is not so much to ask, in the first do i=1 to m i am generating ys from a regression with no shift and i need those to be my first 30 observations always, then in the do until i am generating ys from a shifted regression, they are 3 observation so in each loop i need 33 obs with the last three changing, but as i understood from you, i was just adding the observations from each loop so i have many ys and i am using only the same 33 observation. So if u pls advise me what to do to let yvec1 drop generated ys from each loop after it ends and add the new ones. Thanks in advance. You are a very great help to me.

Rick_SAS
SAS Super FREQ

I am a little confused by yvec versus yvec1,  but I think this is what you want to do. Maybe you'll be able to see the main idea.

1) Outside the first DO loop, allocate yvec to have 33 elements:

yvec = j(33,1);

2) Inside the first DO loop, assign the first 30 elements to the vector y from the no-shift model :

yvec[1:30] = y;

I might not be understanding something (or you have a bug in your program) because y, x1, x2, and epslon1 should have 30 elements, shouldn't they?

3) Inside the DO-UNTIL loop, generate a three-element vector, which I'll call yy, from the shifted regression model. Assign yy to the last three elements of yvec:

yvec[31:33] = yy;

I hope that will get you started. Start with something simple, debug teh simple code, and then gradually add complexities.

Hope this helps.

bibastat
Calcite | Level 5

Sorry to bother u again, and thanx a lot for ur help. I understood the above problem and I was able to fix it, now I did a little adjustment on the codes and the loop doesn't end, here are the new codes:

proc iml;

n=3;

p=2;

m=10;

k=1;

nruns=10000;

yvec=0;

avrlvec=0;

sigma=1;

T=n*(k+m);

x1={1,4,3};

x2={2,3,5};

xp={1 2,4 3,3 5};

l=j(n,1,1);

x=l||x1||x2;

x11=repeat(x1,(m+k));

x22=repeat(x2,(m+k));

i=j(T,1,1);

xx=i||x11||x22;

z=T({ [30] 0 [3] 1});

z0=z#i;

zx1=z#x11;

zx2=z#x22;

xxf=i||z0||x11||zx1||x22||zx2;

beta0=1;

beta1=2;

beta2=3;

alpha=0.05;

alpha1=1-(1-alpha)**2/3;

df1=p+1;

df2=T-2*(p+1);

fdis=finv((1-alpha),df1,df2);

shiftvec={0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5};

nshf=nrow(shiftvec);

do k=1 to nshf;

shift=shiftvec[k,];

c=0;

do i=1 to nruns;

yvec=0;

do i=1 to m;

epslon1=sigma* normal(repeat(-1,n));

y=beta0+(beta1)*x1+(beta2)*x2+epslon1;

yvec=yvec//y;

xty=T(x)*y;

beta=inv(T(x)*x)*xty;

yhat=x*beta;

msej=ssq(y-yhat)/(n-p);

msec=msec//msej;

end;

yvec1=yvec;

mse=sum(msec)/m;

arl=0;

ftest=0;

do until (ftest>fdis);

yvec1=yvec;

epslon=sigma* normal(repeat(-1,n));

y1=beta0+(beta1+shift)*x1+(beta2)*x2+epslon;

yvec1=yvec1//y1;

bhatr=inv(T(xx)*xx)*T(xx)*yvec1[2:34,];

yhatr=xx*bhatr;

err=yvec1[2:34,]-yhatr;

sser=T(err)*err;

bhatf=inv(T(xxf)*xxf)*T(xxf)*yvec1[2:34,];

yhatf=xxf*bhatf;

erf=yvec1[2:34,]-yhatf;

ssef=T(erf)*erf;

msef=T(erf)*erf/(T-2*(p+1));

ftest=(sser-ssef)/((p+1)*msef);

arl=arl+1;

numb=nrow (yvec1);

end;

c=c+arl;

end;

avrl=c/nruns;

avrlvec=avrlvec//avrl;

end;

print yvec1 avrlvec ftest fdis msef numb shift ;

quit;

art297
Opal | Level 21

I'm not versed in iml, but did notice that you had two nested loops both using i as the counter.  I would guess that could be why your loop doesn't end.

bibastat
Calcite | Level 5

Thanx a lot, I am sorry to keep bothering you, but you really save me with your answers

bibastat
Calcite | Level 5

Yes, I figured it yesterday, thanx a lot for your reply.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 10 replies
  • 1514 views
  • 6 likes
  • 3 in conversation