I fi pi(=( f(i)+f(i-1) ) /2 )
1 0.263 (0.263+0)/2 (f(0)=0)
2 0.263 (0.263+0.263)/2
3 0.91 (0.91+0.263)/2
4 0.12 (0.12+0.91)/2
5 0.12 (0.12+0.12)/2
Can I use do loop or other method to calculation pi?
Thanks!
I believe you can achieve the same result without the do loop if you prefer it this way
data have;
input f;
cards;
0.263
0.263
0.91
0.12
0.12
;
data want;
set have;
pi=((f+coalesce(lag(f),0))/2);
run;
Hi @yuwentaiwan
You can use the LAG() function to retrieve the previous value of fi.
The SUM() function is better than the "+" operator in your case as it ignores missing values. So it can easily handle the calculation at the first row, where lag(i) is not defined.
Best,
data want;
set have;
pi = sum(fi,lag(fi))/2;
run;
@yuwentaiwan wrote:
I fi pi(=( f(i)+f(i-1) ) /2 )
1 0.263 (0.263+0)/2 (f(0)=0)
2 0.263 (0.263+0.263)/2
3 0.91 (0.91+0.263)/2
4 0.12 (0.12+0.91)/2
5 0.12 (0.12+0.12)/2
Can I use do loop or other method to calculation pi?
Thanks!
Is this a programming exercise or do you want to USE the value of pi?
If you need to use pi then you would use the function CONSTANT with 'pi' as the argument.
data junk; x=constant('pi'); put x best16.; run;
You can use Proc DS2 or FCMP to compute via recursion.
Or try Proc FCMP SOLVE() function
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.