## How to define a matrix using arithmetic operators?

Occasional Contributor
Posts: 14

# How to define a matrix using arithmetic operators?

It is fine to define the following scalars in SAS:

x1=1/sqrt(5);

x2=2/5;

x3=6**3;

however, syntax errors appear when one tries to define a vector like this:

proc iml;

x={1/sqrt(5) 2/5 6**3};

I never experienced a similar problem in Matlab which I use more frequently than SAS.

It seems that there is a similar problem in SAS data step as well. I know that probably one rarely has to use arithmetic operators in defining

a matrix or in data steps. But I am curious whether this is an easy solution for this.

Posts: 3,215

## Re: How to define a matrix using arithmetic operators?

For iml (not ims) SAS/IML(R) 12.3 User's Guide ( Multiplication Operator, Elementwise:   # ) a sample working with vectors

For the datastep SAS(R) 9.4 Language Reference: Concepts (Syntax for Defining and Referencing an Array)

The major problem looks not to be technical, but getting the syntax and words aligned from one product to another.

---->-- ja karman --<-----
Occasional Contributor
Posts: 14

## Re: How to define a matrix using arithmetic operators?

Thanks for your reply. After reading the materials, I still have not found a way to solve this particular problem using matrix multiplication. I might try it again later.

Contributor
Posts: 45

## Re: How to define a matrix using arithmetic operators?

Hi there, Fellow MATLAB user!

If you would like to define an array use the following syntax:

data test;

/* define array*/
array x{3};

/*populate array values*/
x{1}=1/sqrt(5);

x{2}=2/5;

x{3}=6**3;

run;

/* the resultant fields 'x1, x2, x3' remain in the dataset, however the array structure exists only in the data step, unless reassigned */

data test2;

set test;

/* reconnect x1 x2 and x3 to an array reference*/

array x{3};

/* manipulate array*/

arithmetic = x{2}+x{1}+x{3};

run;

proc print data=test2;

run;

Occasional Contributor
Posts: 14

## Re: How to define a matrix using arithmetic operators?

Thank you for providing this example.

Occasional Contributor
Posts: 14

## Re: How to define a matrix using arithmetic operators?

Here is my example which would be much easier to accomplish in Matlab:

proc iml;

/* an example of diagonalizing a symmetric matrix A*/

/* A wrong attempt to define an orthogonal matrix X*/

*X={-1/3 2/sqrt(5) -2/(3*sqrt(5)), -2/3 0 5/(3*sqrt(5)), 2/3 1/sqrt(5) 4/(3*sqrt(5))} ;

/*define an orthogonal matrix X using concatenations*/

X=(-1/3 || 2/sqrt(5) || -2/(3*sqrt(5))) // (-2/3 || 0 || 5/(3*sqrt(5))) // ( 2/3 || 1/sqrt(5) || 4/(3*sqrt(5)));

A={2 2 -2, 2 5 -4, -2 -4 5};

print A "  " X [format=5.3];

print (X`*X) [format=5.3] [label='X´*X'];

print (eigval(A)) [format=5.3] [label='eigenvalues of A'];

print (X`*A*X)  [format=5.3] [label='X´*A*X'];

quit;

SAS Super FREQ
Posts: 4,171

## Re: How to define a matrix using arithmetic operators?

Welcome to the SAS/IML language.  There are many former MATLAB users on this Support Community, including myself.

To answer your question, curly braces are used to construct a matrix from LITERAL numbers. To build a matrix from EXPRESSIONS, use the horizontal concatenation operator (||) or the vertical concatenation operator, as described in this article:

How to build a vector from expressions - The DO Loop

x = (1/sqrt(5)) || (2/5) || (6**3);

You might also enjoy reading about tips to convert a MATLAB program to SAS/IML: Translating a MATLAB program into the SAS/IML language: A case study - The DO Loop  The article includes a very basic MATLAB to IML cheatsheet that might be helpful to you.

Occasional Contributor
Posts: 14

## Re: How to define a matrix using arithmetic operators?

Thanks for the tips which are exactly right for my problem. I am happy to learn that there are many Matlab users here in the community.

Discussion stats
• 7 replies
• 599 views
• 0 likes
• 4 in conversation