turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- Formula calculation

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-24-2018 06:08 PM

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!

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to user1

03-24-2018 07:43 PM

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);

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Astounding

03-24-2018 11:28 PM - edited 03-24-2018 11:31 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to PGStats

03-26-2018 08:23 PM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to user1

03-27-2018 04:26 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to PGStats

03-27-2018 10:33 PM

Thanks again, PG Stats!!

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Astounding

03-27-2018 11:14 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to user1

03-28-2018 05:32 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Astounding

03-28-2018 03:56 PM

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 (Y_{1}........Y_{N})

Y_{1}=A*(B)^{-1}+ A*(B)^{-2} + A*(B)^{-3} + A*(B)^{-4} +……+ A*(B)^{-10.2}

Y_{2}=A*(B)^{-1}+ A*(B)^{-2} + A*(B)^{-3} + A*(B)^{-4} +……+ A*(B)^{-20.3}

^{…….}

Y_{N}=A*(B)-1+ A*(B)-2 + A*(B)-3 + A*(B)-4 +……+ A*(B)^{-N.5}

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to user1

03-28-2018 04:06 PM

Not clear. What is the term before the last?

PG

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to PGStats

03-28-2018 10:31 PM

Thanks very much, PG!

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

For example,

Y_{1}=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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to user1

03-28-2018 10:35 PM

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.