BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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.
8 REPLIES 8
deleted_user
Not applicable
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
deleted_user
Not applicable
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
deleted_user
Not applicable
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.
deleted_user
Not applicable
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]
deleted_user
Not applicable
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
deleted_user
Not applicable
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?
deleted_user
Not applicable
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.
deleted_user
Not applicable
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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Discussion stats
  • 8 replies
  • 956 views
  • 0 likes
  • 1 in conversation