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
thank you in advance
have a nice day
MC
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;
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;
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;
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;
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!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.