BookmarkSubscribeRSS Feed
humenghu
Calcite | Level 5

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.

7 REPLIES 7
jakarman
Barite | Level 11

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 --<-----
humenghu
Calcite | Level 5

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.

Murray_Court
Quartz | Level 8

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;

humenghu
Calcite | Level 5

Thank you for providing this example.

humenghu
Calcite | Level 5

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;

Rick_SAS
SAS Super FREQ

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

For your particular example, use

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.

humenghu
Calcite | Level 5

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.   

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 7 replies
  • 1410 views
  • 0 likes
  • 4 in conversation