Operations Research topics: SAS/OR,
SAS Optimization, and SAS Simulation Studio

Using do while statement

Reply
N/A
Posts: 0

Using do while statement

I need to make an iterative calculation like this one for example:

u( i+1 )= u( i ) - 0.2;

and I need to make this calculation until | u( i+1) - u( i ) | < 0.001 for example.

Finally I need to obtain the final value of u ( i + 1).

How can I write a "do while" that makes this calculation?

P.S.D. u( i ) can be initilized to any value, let's say 0.
N/A
Posts: 0

Re: Using do while statement

are you sure that this is what your iterative calc should be?[pre] u( i+1 )= u( i ) - 0.2;[/pre]
I don't see how the consecutive elements can be anything other than .02 apart
N/A
Posts: 0

Re: Using do while statement

I agree with Peter, that iterative calc looks odd, just solving for it;

u(i+1)=u(i)+0.2
u(i+1)-u(i)=0.2

which means your loop will never meet the condition you gave, they will always be 0.2 apart by defintion.

Do you mean to increment i by 0.2 and then look for when the the values of u(i) and u(i+1) are within tolerance, like this:

Example:
i=0.2

|u(1.2)-u(0.2)| < 0.001 ?
|u(1.4)-u(0.4)| < 0.001 ?
|u(1.6)-u(0.6)| < 0.001 ?
...

Which I don't think I have ever seen before.

Or do you mean by u(i+1) that u is not a function of i but i is an INDEX of u, which would normally be written as a subscript in mathtype, I have seen it written as u{i} and u{i+1} to distinguish it from u(i) which normally means u is a function of the variable i

So that u{i+1} means 'the next value of u' and not as ''u is evlauated on i+1'. I am not sure that will help since they still will always be 0.2 apart.

If you can clarify or give a different meaning, it would be helpful

Ike Eisenhauer
N/A
Posts: 0

Re: Using do while statement

Ok, I trying to make the code in SAS Language for the EM algorithm, the real formulas are very long and complicated. The EM algorithm that SAS use doesn't apply to my case becuase I'm working with truncated data. Please forget about this specific formula, the important thing is that is has to be an iterative formula. I have to use the result from the actual claculation to obtain the result of the next calculation. So, yes ( i ) or { i } is an index. The iterations must stop when the difference between the last calculation and the actual calculation is less than 1E-10. I think I did that in C++ using arrays but arrays in SAS are not the same thing as in C++. So that's why I'm stock in this part of the code. Thank you very much for your help.
N/A
Posts: 0

Re: Using do while statement

ok, meaningful might be[pre]u( i+1 )= Fn( u( i ) ) ;[/pre] where Fn() is some complex functionality

Then you might do like:[pre] Xu=Fn( u(0) );
do i=1 by 1 until( range( Xu, Ou )> 1e-10 ) ;
Ou = Xu ;
Xu = Fn( u(i) ) ;
end;
[/pre]
N/A
Posts: 0

Re: Using do while statement

nit pick

... range( Xu, Ou ) -lt 1e-10

Where "-lt" is actually the less than symbol. Unfortunately, the code for this forum gets lost when attempting to use less than signs in the text.


Disregard my suggestions about LAG, as it does not appear to be applicable in this case. Message was edited by: Chuck
N/A
Posts: 0

Re: Using do while statement

In SAS you may need to reconstruct the idea slightly differently.

SAS has function called "LAG" which let's you see the previous value of a variable.
LAG is very tricky, though, and requires carefully controlled testing to make sure your code will do what you are expecting it to do.
[pre]
data outdata;
set indata;
new_value = lag(value) * value - 0.2;
run;
quit;
[/pre]
This example will create new_value from the previous observation's "value" and the current observation's "value".

Of course, you can do a simple iteration in SAS.
[pre]
data outdata;
a = 100;
do while a > 0 ;
a = a - 1;
end;
run;
quit;
[/pre]

But the last thing would have been better as
[pre]
data samples;
do i=100 to 0 by -1;
value = ranpoi(0,1);
output;
end;
run;
quit;
[/pre]

So my question, is, what are you trying to do? What is the final goal you are after? What are you trying to accomplish?
N/A
Posts: 0

Re: Using do while statement

A complicated iteration using LAG:
[pre]
data outdata;
set indata end=eod;
new_value = value * lag(value) - 0.2;
out_value = lag(new_value)/new_value ;

if eod then output;
keep out_value;
run;
quit;
[/pre]
I am not going to say anything as to why you might do this, but it demonstrates that a way to obtain the prior value of "new_value" to use in a current calculation.

But, as I said, you have to be very careful with the LAG function.
Start by reading the documentation very carefully.
N/A
Posts: 0

Re: Using do while statement

I'm trying to calculate the estimation for the parameters of a truncated normal distribution. I'm doing it by using the Expectation Maximization Algorithm because with this methodology I can substitute the missing observations that are below some threshold by their conditional expected value.
Ask a Question
Discussion stats
  • 8 replies
  • 310 views
  • 0 likes
  • 1 in conversation