Statistical programming, matrix languages, and more

Create matrix from upper/lower triangles in IML

Reply
New Contributor
Posts: 2

Create matrix from upper/lower triangles in IML


I have two matrices in proc iml that I created from two different SAS data sets.  Let's call them a and b.  I want to create a third matrix (c) by taking the upper-right triangle from matrix a and the lower-left triangle from matrix b.  All of the values in the matrices are numeric.  I tried using a do loop but was unsuccessful.  My matrices are quite large (167x167).  Any ideas?  This was the code that I tried that did not work:

proc iml;

use data1;

read all into a;

use data2;

read all into b;

c=j(167,167,0);


do i=1 to 167;

do j=i+1 to 167;

end;

end;

print c;

quit;

SAS Super FREQ
Posts: 3,624

Re: Create matrix from upper/lower triangles in IML

SAS/IML 12.3 introduced the ROW and COL functions.  To see how they work (and to get code for them if you have an earlier release) see Filling the lower and upper triangular portions of a matrix - The DO Loop By using these functions, it is easy to extract and set the upper or lower triangular portions of a matrix:

upperIdx = loc( col(a) > row(a) );

lowerIdx = loc( col(b) < row(b) );

c[upperIdx] = a[upperIdx];

c[lowerIdx] = b[lowerIdx];

Trusted Advisor
Posts: 1,785

Re: Create matrix from upper/lower triangles in IML

I'm not complaining to you, Rick, I'm complaining to SAS, and I want to point out how much I hate SAS's numbering system.

SAS/IML 12.3 introduced the ROW and COL functions.

I know I have SAS 9.4. I'm 100% certain. I can even look up that it is TS1M0. However, I have no idea if I have SAS/IML 12.3 or SAS/STAT 16.4 or any of the other sub-numberings of SAS sub-products that appear to exist, and furthermore I don't even know where to look this up. Nor do I feel that I should have to remember or even be aware of these sub-version numbers, I wish everyone would talk in terms of SAS 9.4 TS1M0 or SAS 9.3 TS1M1 and then things would be crystal clear.

Anyway, with regards to your specific contribution about the ROW and COL functions, when I look in SAS help (which I get to by pressing F1 from within SAS), I do not see a ROW function or a COL function. I guess this means I do not have SAS/IML 12.3 (does it really mean that? should I now have SAS/IML 12.3? who would know?)

SAS Super FREQ
Posts: 3,624

Re: Create matrix from upper/lower triangles in IML

I share your frustrations, which is why I looked up the releases and wrote a blog post that connects the Base release to the analytical products:

  How old is your version of SAS? Release dates for SAS software - The DO Loop

That post is only 6 months old and has received more than 15,000 views, so I know that we are not alone in wanting to connect the two versioning systems  Bookmark it (I have).

In PC SAS, when I launch SAS the Log says which version of analytical products I have.  I think that information does not appear in EG and other interfaces.

For the record, SAS 9.4TS1M0 corresponds to release 12.1 of the analytical products.

Of course, you can determine your SAS release by submitting

%put SYSVLONG = &SYSVLONG;

Trusted Advisor
Posts: 1,785

Re: Create matrix from upper/lower triangles in IML

For the record, SAS 9.4TS1M0 corresponds to release 12.1 of the analytical products.

According to the blog post you linked to, SAS 9.4TS1M0 corresponds to 12.3, which I obviously don't have if the SAS DOCs don't have the ROW and COL functions.

SAS Super FREQ
Posts: 3,624

Re: Create matrix from upper/lower triangles in IML

See how confusing it is?  I tried to rely on memory instead of looking it up, and I got it wrong!  My apologies.

Yes, you have 12.3, which was a minor maintenance release for the analytical products. The SAS/IML doc was not revised for that minor release because 13.1 followed 5 months later. You can view "What's New in SAS/IML 12.3" as part of the "What's New in 13.1" doc: SAS/IML(R) 13.1 User's Guide

New Contributor
Posts: 2

Re: Create matrix from upper/lower triangles in IML

Thanks for your help, Rick!  Since I do have an older version of SAS, I was able to use your prior post to create the ROW and COL processes.

Ask a Question
Discussion stats
  • 6 replies
  • 836 views
  • 3 likes
  • 3 in conversation