How to Raise a Number to a Power in SAS

Deepanshu Bhalla Add Comment

In SAS, we can raise a number to a power using ** operator. Check out the example below showing how to calculate the square, square root, cube, cube root using SAS.

  • Square : result = variable**2;
  • Square Root : result = variable**(1/2);
  • Cube : result = variable**3;
  • Cube Root : result = variable**(1/3);
Sample SAS Dataset

Let's create a sample dataset for demonstration purpose.

data mydata;
input change;
cards;
2
3
-2
-3
0
;
run;

Calculating Square in SAS

To calculate the square of a number in SAS, use the ** operator with the power of 2.

data example;
set mydata;
sq_change = change**2;
proc print;
run;
Calculating Square in SAS

Calculating Square Root in SAS

To calculate the square root of a number in SAS, you can use either the sqrt function or ** operator with the power of 0.5.

data example;
set mydata;
sqrt_change = change**(1/2);
sqrt_change2 = sqrt(change);
proc print;
run;
Calculating Square Root in SAS

If you want zero instead of a missing value when calculating the square root of a negative number, you can use the MAX function to compare the number with zero.

data example;
set mydata;
sqrt_change = max(0, change)**(1/2);
run;

Calculating Cube in SAS

To calculate the cube of a number in SAS, you can use the ** operator with the power of 3.

data example;
set mydata;
cube_change = change**3;
proc print;
run;
Calculating Cube in SAS

Calculating Cube Root in SAS

To calculate the cube root of a number in SAS, you can use the ** operator with the power of (1/3).

data example;
set mydata;
cbrt_change = change**(1/3);
proc print;
run;
Calculating Cube Root in SAS
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "How to Raise a Number to a Power in SAS"
Next → ← Prev