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
- /
- Does SAS have a function to calculate (-1)**(1/3)?

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

12-01-2010 09:30 AM

Hello,

Recently I hit a very simple and surprising SAS issue, namely calculating powers for negative base like:

(-1)**(1/3) or (-1)**0.2345 etc.

For this simple SAS (EG 4.3) program:

[pre]

data a;

x=(-2)**0.2345;

x1=-2**0.2345;

run;

[/pre]

I got the following in the LOG:

[pre]

17 data a;

18 x=(-2)**0.2345;

NOTE: Invalid argument(s) to the exponential operator "**".

19 x1=-2**0.2345;

20 run;

[/pre]

Of course, I understand that SAS goes into a trouble calculating LOG from negative numbers, and I can use a logic to overcome it. However, even a table calculator crunches these powers easily.

I'm pretty sure that this issue has been raised in the past. I would like to know if there is a SAS function to calculate such powers?

Sincerely,

SPR

Recently I hit a very simple and surprising SAS issue, namely calculating powers for negative base like:

(-1)**(1/3) or (-1)**0.2345 etc.

For this simple SAS (EG 4.3) program:

[pre]

data a;

x=(-2)**0.2345;

x1=-2**0.2345;

run;

[/pre]

I got the following in the LOG:

[pre]

17 data a;

18 x=(-2)**0.2345;

NOTE: Invalid argument(s) to the exponential operator "**".

19 x1=-2**0.2345;

20 run;

[/pre]

Of course, I understand that SAS goes into a trouble calculating LOG from negative numbers, and I can use a logic to overcome it. However, even a table calculator crunches these powers easily.

I'm pretty sure that this issue has been raised in the past. I would like to know if there is a SAS function to calculate such powers?

Sincerely,

SPR

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

12-01-2010 10:39 AM

Yes, it has been raised in the past. Go to Google and type

[pre]

sas Invalid argument(s) to the exponential operator "**"[/pre]

in the search box.

You may find the macro shown in this sas-l post from Sept 2000 helpful: http://www.listserv.uga.edu/cgi-bin/wa?A2=ind0009b&L=sas-l&P=3965

[pre]

sas Invalid argument(s) to the exponential operator "**"[/pre]

in the search box.

You may find the macro shown in this sas-l post from Sept 2000 helpful: http://www.listserv.uga.edu/cgi-bin/wa?A2=ind0009b&L=sas-l&P=3965

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

Posted in reply to Tim_SAS

12-01-2010 11:27 AM

solves by just rearranging the parentheses

from

(-1)**(1/3)

to

-(1**(1/3))

to move the minus away from the fractional power arithmetic

from

(-1)**(1/3)

to

-(1**(1/3))

to move the minus away from the fractional power arithmetic

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

Posted in reply to Tim_SAS

12-01-2010 12:28 PM

Hello PeterC,

Thanks. I think that the following construction:

[pre]

y=SIGN(x)*ABS(x)**power;

[/pre]

works for most of the cases except for power=1/2n, n=1,.... Simple logic can be used to catch these exceptions and assign missing to y.

Sincerely,

SPR

Thanks. I think that the following construction:

[pre]

y=SIGN(x)*ABS(x)**power;

[/pre]

works for most of the cases except for power=1/2n, n=1,.... Simple logic can be used to catch these exceptions and assign missing to y.

Sincerely,

SPR

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

12-01-2010 01:12 PM

SPR

I don't see why there is an issue about "inverse evens" like powers 1/2, 1/4.

Once the sign(x) is extracted the exponentiation with any fractional power of the abs(x) positive number, delivers a single positive answer.

As I suggested, removing that minus removes the problem.

As suggested 10 years ago by Ian Whitlock

y=SIGN(x)*ABS(x)**power;

works.

Is it not appropriate to remove the minus (as in sign(x) *abs(x) **power )?

peter

(sorry I'm just perplexed at the logic that suggests there is logical difference between 1/2 and 1/3 in the arithmetic when I would see no similar difference between .24, .25, .26 and .3, .3333)

I don't see why there is an issue about "inverse evens" like powers 1/2, 1/4.

Once the sign(x) is extracted the exponentiation with any fractional power of the abs(x) positive number, delivers a single positive answer.

As I suggested, removing that minus removes the problem.

As suggested 10 years ago by Ian Whitlock

y=SIGN(x)*ABS(x)**power;

works.

Is it not appropriate to remove the minus (as in sign(x) *abs(x) **power )?

peter

(sorry I'm just perplexed at the logic that suggests there is logical difference between 1/2 and 1/3 in the arithmetic when I would see no similar difference between .24, .25, .26 and .3, .3333)

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

Posted in reply to Peter_C

12-01-2010 03:34 PM

PeterC

1) "I don't see why there is an issue about "inverse evens" like powers 1/2, 1/4."

(-1)**(1/2)=SQRT(-1) which is obvious problem. SQRT(-1)=i (complex number). The same problem for other powers.

2) "Is it not appropriate to remove the minus (as in sign(x) *abs(x) **power )?"

(-1)**(1/2) converts to SIGN(-1)*ABS(-1)**(1/2)=-1 that is not correct, because the correct result is i (complex number).

SPR

1) "I don't see why there is an issue about "inverse evens" like powers 1/2, 1/4."

(-1)**(1/2)=SQRT(-1) which is obvious problem. SQRT(-1)=i (complex number). The same problem for other powers.

2) "Is it not appropriate to remove the minus (as in sign(x) *abs(x) **power )?"

(-1)**(1/2) converts to SIGN(-1)*ABS(-1)**(1/2)=-1 that is not correct, because the correct result is i (complex number).

SPR

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

12-01-2010 04:25 PM

The fractional powers serve a purpose for me that makes complex numbers seem alien. Most meaningful example occurs when generating periodic rates in compound interest for example providing monthly and quarterly equivalent rates for annual data. Nominally the method discussed above could provide a derived rate over a third of a year but not for half or quarter.

Must be something wrong with my model which is approximately like:

Interval_rate = (1+annual)**[1/interval] -1 ;

If "annual" is very bad (1+annual) might become negative

If interval is 3 or 4 I expect logically similar results and cannot just ban intervals which are even.

I imagine there are models in other fields which need to solve this kind of arithmetic in a consistent way without just saying

It's "i"

Peter

Must be something wrong with my model which is approximately like:

Interval_rate = (1+annual)**[1/interval] -1 ;

If "annual" is very bad (1+annual) might become negative

If interval is 3 or 4 I expect logically similar results and cannot just ban intervals which are even.

I imagine there are models in other fields which need to solve this kind of arithmetic in a consistent way without just saying

It's "i"

Peter

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

Posted in reply to Peter_C

12-01-2010 04:38 PM

There is an obvious problem with this formula for example for interval=4. To solve it, it is necessary to return to the method used to derive this formula and to see what will be for even intervals. I do not know how this formula has been derived and hence can not be more specific.

SPR

SPR

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

12-01-2010 06:18 PM

the problem is with the data being out of valid range for the model.

Even though "odd" fraction powers might provide a real root there are also "imaginary" roots (- since you wish to be rigorously mathematical I think that was the terminology when I learnt complex maths in school....) but mine was not imaginary but a real problem: re-basing a measure of the rate of increase (or decline).. So as that terminology switched from increase to decline, the negative sign was removed to "outside" the calculation and into the terminology...

Even though "odd" fraction powers might provide a real root there are also "imaginary" roots (- since you wish to be rigorously mathematical I think that was the terminology when I learnt complex maths in school....) but mine was not imaginary but a real problem: re-basing a measure of the rate of increase (or decline).. So as that terminology switched from increase to decline, the negative sign was removed to "outside" the calculation and into the terminology...

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

Posted in reply to Tim_SAS

12-01-2010 12:32 PM

Hello Tim@SAS,

Thank you, it was very helpful. I think that this macro has a limited scope of application. However, the idea of a combination of SIGN and ABS from this sas-l post is much better solution.

Sincerely,

SPR

Thank you, it was very helpful. I think that this macro has a limited scope of application. However, the idea of a combination of SIGN and ABS from this sas-l post is much better solution.

Sincerely,

SPR