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