Solved
Contributor
Posts: 35

# SAS BASE: from TRIANGLE UPPER MATRIX to Squared Matrix - NO IML

Hello Everyone,

i have a triangle upper matrix like that:

``````data triangle_upper_matrix;
infile datalines delimiter=',';
input varname \$ var1 \$ var2 \$ var3 \$;
datalines;
var1, x11, x12, x13
var2,      , x22, x23
var3,      ,       ,x33
;;;
run;

``````

and i want calculate the squared matrix, in order to the value in the lower portion of the matrix, like:

``````data Squared;
infile datalines delimiter=',';
input varname \$ var1 \$ var2 \$ var3 \$;
datalines;
var1, x11, x12, x13
var2, x12, x22, x23
var3, x13, x23, x33
;;;
run;``````

any suggests?

RESTRICTION: usually i used R but in my situation i havn't and also i don't have proc IML and after that i need to develop the same thing but in a dinamically way with a MACROPROGRAM where the only input is a dataset and without know number of variable and work

have a nice day

MC

Martino Crippa

Accepted Solutions
Solution
‎06-28-2016 11:07 AM
Posts: 3,852

## Re: SAS BASE: from TRIANGLE UPPER MATRIX to Squared Matrix - NO IML

When your variables are numeric it simplifies even more.  After all the name of the procedure is TRANSPOSE.

``````data upper;
infile datalines dsd;
input varname \$ var1 var2 var3;
datalines;
var1,11,12,13
var2,,22,23
var3,,,33
;;;
run;
proc transpose data=upper out=lower name=varname;
id varname;
run;
data square;
update lower upper;
by varname;
run;
``````

All Replies
Posts: 3,852

## Re: SAS BASE: from TRIANGLE UPPER MATRIX to Squared Matrix - NO IML

[ Edited ]

``````data upper;
infile datalines dsd;
input (varname var1 var2 var3)(\$);
datalines;
var1,x11,x12,x13
var2,,x22,x23
var3,,,x33
;;;
run;
proc transpose data=upper(drop=varname) out=lower prefix=var name=varname;
var var:;
run;
data square;
update lower upper;
by varname;
run;
``````

Solution
‎06-28-2016 11:07 AM
Posts: 3,852

## Re: SAS BASE: from TRIANGLE UPPER MATRIX to Squared Matrix - NO IML

When your variables are numeric it simplifies even more.  After all the name of the procedure is TRANSPOSE.

``````data upper;
infile datalines dsd;
input varname \$ var1 var2 var3;
datalines;
var1,11,12,13
var2,,22,23
var3,,,33
;;;
run;
proc transpose data=upper out=lower name=varname;
id varname;
run;
data square;
update lower upper;
by varname;
run;
``````
Super User
Posts: 10,766

## Re: SAS BASE: from TRIANGLE UPPER MATRIX to Squared Matrix - NO IML

My code is faster than Data _null_( John King ):

``````data triangle_upper_matrix;
infile datalines delimiter=',';
input varname \$ var1 \$ var2 \$ var3 \$;
datalines;
var1, x11, x12, x13
var2,      , x22, x23
var3,      ,       ,x33
;;;
run;
%let dsid=%sysfunc(open(triangle_upper_matrix));
%let nobs=%sysfunc(attrn(&dsid,nlobs));
%let nvar=%eval(%sysfunc(attrn(&dsid,nvars))-1);
%let dsid=%sysfunc(close(&dsid));

data want;
set triangle_upper_matrix;
array x{&nobs,&nvar} \$ _temporary_;
array y{*} \$ var1-var3;

do i=1 to dim(y);
x{_n_,i}=y{i};
end;

do i=1 to _n_-1;
y{i}=x{i,_n_};
end;
drop i;
run;
proc print noobs;run;

``````

🔒 This topic is solved and locked.