Hello,
( I'm sorry I didn't clearly describe my questions, so I edit it a little bit, hope this time it's much clear.)
I'm trying to do a iteration in a macro with do loop. But still have problems about using IF THEN to end the loop.
The Logic here is: I have a initial K value, and after several calculations I got the Kt. If abs(Kt-K)>0.0001 then let the Kt be the K and do the calculation again until the last step to get the Kt, if abs(Kt-K)<=0.0001 then end the loop and get the Kt.
The equations I used to get Kt is Kt = 1/n * sum(log(k*x/sigma-k*theta/sigma+1)), where x is my sample, theta and sigma are constant. And at first what i was trying to do is to give a initial value of K, then calculate the Kt, and let the Kt be the new K, then do the loop for 100 times. As a result, I could get 100 values of Kt. And The code I use is as following:
* First clean up my dataset to get my sample X, constant theta and sigma, and give the initial K ;
data test1;
set test;
x=prep_ad;
theta=P75_pow;
sigma=1/f75_pow;
K=0.5;
keep station x theta sigma K;
run;
* Then I use macro to let it run 100 times ;
%macro EstimateK;
%do i = 2 %to 100;
* Calculate the value (log(K*x/sigma-K*theta/sigma+1)) for each X;
data test_after&i;
set test_after1;
LN =LOG((K / sigma) * x + 1 - K * theta / sigma);
run;
* Get the sum of LN of the former step and the total number of X (which is n in the equation). And then calculate Kt;
proc means data=test&i noprint;
var LN;
by station;
output out=test_sum sum=SumLN;
run;
data test_sum;
set test_sum;
N=_FREQ_;
Kt=SumLN/N;
keep station Kt;
run;
* Record Kt for each run;
data test_processK&i;
set test_sum;
K&i=K;
keep station K&i;
run;
* Merge the new Kt to the old dataset;
data test&i;
set test&i;
if _n_ =1 then set test_sum;
run;
quit;
%end;
%mend;
%EstimateK;
* After run the macro, I merge all the Kt into one dataset;
data test_processK;
merge test_processK2-test_processK100;
by station;
run;
This code force the loop run 100 times, but I'm trying to give it a condition, which is that the difference between K and Kt is less than 0.0001. Once the condition is met, then jump out of the loop, else do it until 100 time.
My question is I don't know where should I put the IF condition in the macro.
Hope anyone could help me with this problem!
Thank you in advance!
Best,
Hua
... View more