BookmarkSubscribeRSS Feed
Xamius32
Calcite | Level 5

For example, Column 1 Row 1 would be a total. The Column 2 Row 1 plus Column 1 Row 2, then COL3 Row1+Col2 Row 2+ Col1 Row3, etc. For however many columns I have. It isnt a square matrix so once the columns exceed the rows it wouldnt match to the first column.

Header 1Header 2Header 3Header 4Header 5
ABCD
ABCD
BCD

So for example above, I would want the totals of A, B, C, D etc.

I have the matrix, now just not sure how to add up what I need.

Any ideas?

3 REPLIES 3
Xamius32
Calcite | Level 5

Thinking about this more, it could be easier if I could figure out how to shift over rows by X amount. So for row 2, I would shift it over by 1, and the last observation would be deleted. Is that possible?

IanWakeling
Barite | Level 11

I have a function to do exactly this.   It creates the 'shift' that you mention by manipulating the row index numbers of a matrix in its sparse format (details of the format can be found in the documentation for the SPARSE function).  The result when converted back to normal form, with the FULL function, is a new matrix with one row for every antidiagonal.  Finally it is a simple matter to sum the rows of this new matrix.

start AntiDiagSum( x );
  nr = nrow( x );
  nc = ncol( x );
  s = sparse( x );
  s[ , 2] = s[ , 2] + s[ , 3] - 1;
  return( full( s , nr + nc - 1, nc )[ , +] );
finish;

/* get example 3x5 matrix */
a = shape(1:15, 3, 5);
print a;

/* required sums are from the 2nd to 5th antidiagonals */
b = AntiDiagSum(a)[2:5];
print b;

Ksharp
Super User

I don't how to do it with IML . But I can get it via data step. Maybe Rick can give IML solution.

data have;
input a1-a5;
cards;
99 1 2 3 4
1 2 3 4 99
2 3 4 99 99
;
run;
data temp;
 set have;
 array x{*} a1-a5;
  do j=dim(x)-_n_+1 to 1 by -1;
   x{j+_n_-1}=x{j};
  end;
  do i=1 to _n_-1;
   x{i}=.;
  end;
drop i j;
run;
proc means data=temp sum;
var a2-a5;
run;

Xia Keshan

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
  • 3 replies
  • 1129 views
  • 1 like
  • 3 in conversation