BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MC1985
Obsidian | Level 7

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

Martino Crippa
1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

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;

View solution in original post

3 REPLIES 3
data_null__
Jade | Level 19

 

 

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;

Capture.PNG 

data_null__
Jade | Level 19

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;
Ksharp
Super User

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;
 
 

x.png

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!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1171 views
  • 3 likes
  • 3 in conversation