Not applicable
Posts: 1

# How can I loop a value of a variable created in each iteration of a DO loop?

I am a beginner SAS user.  In this simulation, there is an initial value of the variable called "p", but there will be a new value (called "newp" in code below") calculated by SAS for each iteration in the simulation.  How can I get this value that is created in each iteration to be used in the iteration that follows?  See code below.

That is, when gen=1, the value of "p" is 0.00005.  Based on the results of the simulation a "newp" will be calculated using the entered formula.  This "newp" should be used for gen=2, where again, another different "newp" is calculated and should be used in gen=3, and so forth.

data a;

do gen=1 to 10;

seed=47639087;

expon=2*ranuni(seed) + 4;

n=round(10**expon,1);

p=.00005;

q=1-p;

NumRR = round(n*p*p,1);

NumSR = round(2*n*p*q,1);

NumSS = round(n*q*q,1);

newp=(NumRR + .5*NumSR) / (Numrr + numsr  + numss);

diffp=p-newp;

newq=(NumSS + .5*NumSR)/ (Numrr + numsr  + numss);

diffq=q-newq;

output;

end;

Super Contributor
Posts: 578

## Re: How can I loop a value of a variable created in each iteration of a DO loop?

data a;

newp=.00005;

do gen=1 to 10;

seed=47639087;

expon=2*ranuni(seed) + 4;

n=round(10**expon,1);

p=.newp;

q=1-p;

NumRR = round(n*p*p,1);

NumSR = round(2*n*p*q,1);

NumSS = round(n*q*q,1);

newp=(NumRR + .5*NumSR) / (Numrr + numsr  + numss);

diffp=p-newp;

newq=(NumSS + .5*NumSR)/ (Numrr + numsr  + numss);

diffq=q-newq;

output;

end;

Discussion stats