BookmarkSubscribeRSS Feed
user1
Calcite | Level 5

Hi,

 

Can anyone help the calculation of Y below using SAS code?

 

Y=A*(B)-1+ A*(B)-2 + A*(B)-3 + A*(B)-4 +……+ A*(B)-N

 

Thanks a lot!

11 REPLIES 11
Astounding
PROC Star

Within a DATA step, and assuming you have variables A, B, and N:

 

y=0;

do k=1 to n;

   y + a*b**(-k);

end;

 

If B is actually an expression rather than a variable, put it in parentheses:

 

y + a*(b)**(-k);

PGStats
Opal | Level 21

Or this, which should be faster:

 

    u = 0;
    v = 1/b;
    do k = 1 to n;
        u = (1 + u) * v;
        end;
    y = a*u;
PG
user1
Calcite | Level 5

Thanks for you both for providing the answer, much appreciated! PG Stats, since the power is negative, should the sas code be revised?

 

as  u = 0;
    v = 1/b;
    do k = -1 to -n; u = (1 + u) * v; end;
    y = a*u;

 

Thanks and regards.

PGStats
Opal | Level 21

No. k is only a counter, not the exponent. The exponent comes from the number of multiplications by v (= 1/b, = b**-1), i.e. the negative exponent results from multiplying by a negative power of b.

PG
user1
Calcite | Level 5

Thanks again, PG Stats!!

user1
Calcite | Level 5

Thanks! How about if N has one decimal place, e.g. 14.1?

Astounding
PROC Star

What is the first term in the equation if N has one decimal place?

user1
Calcite | Level 5

In the equation, everything is the same, only if N is not an integer, the example is below and I need to calculate for each record (Y1........YN)

 

Y1=A*(B)-1+ A*(B)-2 + A*(B)-3 + A*(B)-4 +……+ A*(B)-10.2

Y2=A*(B)-1+ A*(B)-2 + A*(B)-3 + A*(B)-4 +……+ A*(B)-20.3

…….

YN=A*(B)-1+ A*(B)-2 + A*(B)-3 + A*(B)-4 +……+ A*(B)-N.5

PGStats
Opal | Level 21

Not clear. What is the term before the last?

PG
user1
Calcite | Level 5

Thanks very much, PG!

the term before the last is: A*(B)-N+1

For example,

Y1=A*(B)-1+ A*(B)-2 + A*(B)-3 + A*(B)-4 +……+ A*(B)-9 +A*(B)-10.2

 

When I run the above sas code, the decimal 0.2 from the last term is missing.

user1
Calcite | Level 5

sorry, it is not -N+1, it should be integer -N+1, for example if N is 10.2, then the term before the last one should be A*(B)-9 , not 9.2.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 11 replies
  • 1839 views
  • 2 likes
  • 3 in conversation