## Does SAS have a function to calculate (-1)**(1/3)?

Super Contributor
Posts: 366

# Does SAS have a function to calculate (-1)**(1/3)?

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
Super Contributor
Posts: 394

## Re: Does SAS have a function to calculate (-1)**(1/3)?

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
Valued Guide
Posts: 2,191

## Re: Does SAS have a function to calculate (-1)**(1/3)?

solves by just rearranging the parentheses
from
(-1)**(1/3)
to
-(1**(1/3))
to move the minus away from the fractional power arithmetic
Super Contributor
Posts: 366

## Re: Does SAS have a function to calculate (-1)**(1/3)?

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
Valued Guide
Posts: 2,191

## Re: Does SAS have a function to calculate (-1)**(1/3)?

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)
Super Contributor
Posts: 366

## Re: Does SAS have a function to calculate (-1)**(1/3)?

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
Valued Guide
Posts: 2,191

## Re: Does SAS have a function to calculate (-1)**(1/3)?

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
Super Contributor
Posts: 366

## Re: Does SAS have a function to calculate (-1)**(1/3)?

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
Valued Guide
Posts: 2,191

## Re: Does SAS have a function to calculate (-1)**(1/3)?

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...
Super Contributor
Posts: 366

## Re: Does SAS have a function to calculate (-1)**(1/3)?

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
Discussion stats
• 9 replies
• 1754 views
• 0 likes
• 3 in conversation