0

I need to raise every number in an array (a column in a SAS dataset) to a certain power. I've been told that this can be done in IML, after writing the contents of the column to a matrix. However, a matrix must apparently be square to be raised to a power in IML, and my data are very much not a square matrix. What is the procedure and the syntax for raising one column of numbers to a power?

Sorry for asking about such simple things, I'm as new to SAS as can be.

Mhoram
  • 421
  • 2
  • 6
  • 13
  • I understand I should probably have said "range" instead of "array". – Mhoram Oct 21 '15 at 19:27
  • 1
    @Stu's answer below is correct. If you are in IML and attempt Mat**2, then you are attempting a matrix multiplication (Mat x Mat, or Mat squared). You wanted an element-wise, whose operator is #. So Mat##2 will do an element wise power. – DomPazz Oct 21 '15 at 19:50

3 Answers3

3

No need to apologize. SAS is a very different thought process that takes a lot of practice. You're asking all the right questions.

SAS is an iterative language. The array of numbers you are speaking of sounds like many individual observations within a dataset. SAS performs operations on values one row at a time within each dataset, then moves onto the next row once it hits the bottom of a data step.

Assuming your dataset looks like this:

 var
 1
 2
 3
 4

If you wanted to square each number, you would create a new variable (or you can overwrite the current variable, your choice) and use the ** exponent operator.

data want;
    set have;
    var_squared = var**2;
run;

In this step, SAS creates a dataset want, reads the dataset have, creates a new numeric variable of length 8 named var_squared, assigns it a value of var*var, then outputs. It repeats the read-calculate-write cycle for each row until it hits the end of file marker. Note that output is implied automatically. When you do not specify output, SAS will automatically write to the new dataset once it hits run.

Your new dataset will look like this:

var var_squared
1   1
2   4
3   9
4   16
Stu Sztukowski
  • 10,597
  • 1
  • 12
  • 21
0

As you've discovered, the SAS/IML language provides many more types of multiplication and "raise to power" operations than the familiar scalar operations. You can read about different ways to multiply in the SAS/IML language.

For most operations, the SAS/IML language distinguishes between elementwise operations and vector or matrix operations. For your use case, you should use the elementwise power operator (##) which will raise each element of a vector or matrix to a power. For example, to compute the square of the height of each student in a data set, you can use:

proc iml;
use sashelp.class;
read all var {"Height"};
close sashelp.class;

sqHt = Height##2;   /* raise each element to 2nd power */
print Height sqHt;
Rick
  • 1,210
  • 6
  • 11
0

Thanks! I used the elementwise operator, and it worked (the software did not object this time), although another issue arose (about which I'll ask in a separate question).

Mhoram
  • 421
  • 2
  • 6
  • 13