DATA Step, Macro, Functions and more

Transposing a matrix using a do loop to create a symmetric matrix from a lower triangular

Reply
Contributor
Posts: 67

Transposing a matrix using a do loop to create a symmetric matrix from a lower triangular

Hello!

I have a lower triangular correlation matrix matrix in a data set. I need to turn this lower triangular correlation matrix (it is in a data set right) into a square symmetric correlation matrix. I was wondering the best way to do this? I tried using the following code, but unfortunately it just prints the symmetric data set to my output whereas, I need to print it to a new data set. I could also write a do loop; I have 19 variables, xGT1 through xGT19. Basically

I want xGT1 through xGT19 , so a 19x19 matrix. How could I do this? Because it's a correlation matrix, I just have 1's on the diagonal. Below is the code I used, using the sqrvech command. Any help is appreciated.

Thank you soooo much!

proc iml;

use pcorr1;

read all var _NUM_ into Dist;

close pcorr1;

d = vech(Dist) ;

Dist = sqrvech(d) ;

print Dist[format=7.5

i was thinking of doing something like

data new; set old;

     array gt {19} xgt1 - xgt19;

     for i = 1 to 19;

     for j = 1 to 19;

     if xgt{i,j} = . then xgt{i,j} = xgt{j,i};

but I am not sure.... here I don't have two indices for i and j, I just have one index.

thanks,Gina

Respected Advisor
Posts: 4,930

Re: Transposing a matrix using a do loop to create a symmetric matrix from a lower triangular

Use transpose and update. I assume your data is a typical TYPE=CORR dataset :

data have;
length _TYPE_ _NAME_ $8;
array c{4};
_TYPE_ = "CORR";
do i = 1 to dim(c);
     call missing(of c{*});
     _NAME_ = vname(c{i});
     do j = 1 to i-1;
          c{j} = 0.1*(i-j);
          end;
     c{i} = 1;
     output;
     end;
drop i j;
run;

proc transpose data=have out=havet; copy _TYPE_; run;

data want;
update have havet;
by _TYPE_ _NAME_;
run;

PG

Message was edited by: PG - Added the code in red .

PG
Ask a Question
Discussion stats
  • 1 reply
  • 547 views
  • 0 likes
  • 2 in conversation