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

Hi all,

 

I have a nxn matrix (for simplicity assuming n=3) like following:

 

proc iml;
A = {0.1 0.2 0.5, 0.2 0.4 0.6, 0.2 0.3 0.8};

I have another table - a reference table, which specifies which values from this nxn (n=3) matrix needs to be written to a new data table. that table is following:

 

data output_table;
infile datalines dsd;
length FieldName $20 FieldSourceTable $20;
input Matrix_Id Row_Order Column_Order IsZero FieldName	FieldSourceTable;
datalines;
3,    1,   1,	1,	.,	.
3,    1,   2,	0,	result1,	table2
3,    1,   3,	0,	result2,	table2
3,    2,   1,	1,	.,		.
3,    2,   2,	0,	sum1,	table2
3,    2,   3,	0,	sum2,	table2
3,    3,   1,	0,	prod1,		table2
3,    3,   2,	0,	prod2,		table2
3,    3,   3,	1,	.,		.
;

Assume the matrix A is my Matrix_ID 3. So, I need to create a new table called table 2 with the names specified in 'FieldName' column in the reference table and write specific values from the matrix to the table (e.g. (1,2)th element to the column 'result1').

 

For this specific example, the output should look like:

 

result1result2sum1sum2prod1prod2
0.20.50.40.60.20.3

 

How do I do this in iml? Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
ss59
Obsidian | Level 7

I've been able to figure this out:

 

proc sort data= output_table out= output_table;
by Row_Order Column_Order;
quit;

proc iml;
use output_table;  /* parameterization table for matrix 3 (output table)*/
read all var {Column_Order Row_Order FieldName};
close;

idx = loc(FieldName ^= " ");

refNames = FieldName[idx];

A = {0.1 0.2 0.5, 0.2 0.4 0.6, 0.2 0.3 0.8}; /*A is the full matrix to write*/

C = A[idx];
C = t(C);
mattrib C colname=refNames;

print C;

create data_to_write from C[colname = refNames];
append from C; /** create data set **/
close data_to_write;

View solution in original post

1 REPLY 1
ss59
Obsidian | Level 7

I've been able to figure this out:

 

proc sort data= output_table out= output_table;
by Row_Order Column_Order;
quit;

proc iml;
use output_table;  /* parameterization table for matrix 3 (output table)*/
read all var {Column_Order Row_Order FieldName};
close;

idx = loc(FieldName ^= " ");

refNames = FieldName[idx];

A = {0.1 0.2 0.5, 0.2 0.4 0.6, 0.2 0.3 0.8}; /*A is the full matrix to write*/

C = A[idx];
C = t(C);
mattrib C colname=refNames;

print C;

create data_to_write from C[colname = refNames];
append from C; /** create data set **/
close data_to_write;

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!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 1 reply
  • 770 views
  • 2 likes
  • 1 in conversation