BookmarkSubscribeRSS Feed
arbongard
Calcite | Level 5

I am suppose only be able to put in 2x2 matrices and find the determinate if the matrices is a 2x2 if it is a 1x1 or a 3x3 I am suppose to print an error and if it is singular i am suppose to print another error.

What am I doing wrong?

Proc IML;

a = {1 2, 3 4};

b = {1 1, 1 1};

c = {1 2 3, 4 5 6, 7 8 9};

d = {5};

start invers(a);

  nr = nrow(a);

  nc = ncol(a) ;

  if nr=2 & nc=2 then do;

    i = det(a);

    b = INV(a);

        if i ^= 0 then return(b);

        else if i = 0 then print("Singular");

        end; else print("error");

finish;

w = invers(a);

x = invers(b);

y = invers(c);

z = invers(d);

print w, x, y, z;

5 REPLIES 5
IanWakeling
Barite | Level 11

Inverting a singular matrix causes an error so the INV function needs to be moved inside the 'if'.  To avoid further errors it is necessary to always return something from the function module.  I usually use syntax like return(.) or return(0) when an error occurs.  Then the calling program can test for these return codes and know that an error has occurred.

arbongard
Calcite | Level 5

inside what if statement?

IanWakeling
Barite | Level 11

Don't attempt to invert the matrix unless it has passed the  i^=0  test.

arbongard
Calcite | Level 5

so would this work?

if i ^=0 then return (a**(-1))

IanWakeling
Barite | Level 11

Yes that should work, or return(inv(a)).

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
  • 5 replies
  • 1098 views
  • 3 likes
  • 2 in conversation