BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
arbongard
Calcite | Level 5

How would I write a subrountine that takes 2 parameters (a constant and a square matrix). Then adds that constant to the every element of the diagonal of the matrix, and passes the results back in a new parameter?

Then out puts the new matrix after the subtracted constant

I have the general idea but getting stuck on the diagonal part.

1 ACCEPTED SOLUTION

Accepted Solutions
Hutch_sas
SAS Employee

A quick way to add a constant to the diagonal elements of a matrix:

/* add B to diagonal of a square matrix A */

C = A + diag(  j(nrow(A),  1,   B)  );

View solution in original post

1 REPLY 1
Hutch_sas
SAS Employee

A quick way to add a constant to the diagonal elements of a matrix:

/* add B to diagonal of a square matrix A */

C = A + diag(  j(nrow(A),  1,   B)  );

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1 reply
  • 993 views
  • 0 likes
  • 2 in conversation