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
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.