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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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