BookmarkSubscribeRSS Feed
yuwentaiwan
Calcite | Level 5

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!

4 REPLIES 4
Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag
ed_sas_member
Meteorite | Level 14

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;
ballardw
Super User

@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;
RichardDeVen
Barite | Level 11

You can use Proc DS2 or FCMP to compute via recursion.

Or try Proc FCMP SOLVE() function

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 817 views
  • 3 likes
  • 5 in conversation