20

I may be missing something obvious, but how do you calculate 'powers' in SAS?

Eg X squared, or Y cubed?

what I need is to have variable1 ^ variable2, but cannot find the syntax... (I am using SAS 9.1.3)

Allan Bowe
  • 12,306
  • 19
  • 75
  • 124
  • 2
    SAS syntax doesn't always follow the established conventions (probably because the syntax is so old). The <>-operator is particulary nasty, as it's interpreted as max and because SAS interprets 0 and missing as false and anything else as true. – Ville Koskinen Oct 21 '09 at 17:11

3 Answers3

32

got it! there is no function.

you need to do:

variable1 ** variable2;

Allan Bowe
  • 12,306
  • 19
  • 75
  • 124
6
data t;
  num = 5;
  pow = 2;
  res = num**pow;
run;
proc print data = t;
run;
Triad sou.
  • 2,969
  • 3
  • 23
  • 27
DaveW
  • 69
  • 1
  • 1
1

Use the POWER function and, if necessary, the CONSTANT function.

nbr_squared = power(nbr, 2);
nbr_cubed = power(nbr, 3);
E_to_the_power_2 = power(constant('E'),2);
  • Not sure why this got two upvotes, but it's incorrect; there is no POWER function in base SAS, unless you implement it yourself. The ** operator is the correct answer. – Joe Nov 16 '19 at 03:06
  • @Joe, `power()` is valid in `proc ds2` now. – whymath Jul 07 '20 at 03:22
  • 1
    @whymath Perhaps, but this is certainly not a PROC DS2 question. – Joe Jul 07 '20 at 03:27