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

I wanted to create a matrix with nested loops where elements have numeric values but also concatenated string values, however I get this error: "ERROR: (execution) Invalid argument to function." 

 

I built a small example that expresses the problem:

proc iml;
/* data example */
pds = {0.3, 0.4, 0.8};
M = J(3,1,0); do i=1 to 3; M[i,1] = 'PD' + char(pds[i]); end; print M; quit;

As I understand it: it is not possible to write a string to a matrix element?

 

Thank you for suggestions how to solve this!

1 ACCEPTED SOLUTION

Accepted Solutions
IanWakeling
Barite | Level 11

The matrix M would have to be defined as a character for this to work.  For example:

 

proc iml;
/* data example */
pds = {0.3, 0.4, 0.8};

M = J(3,1,'       ');
do i=1 to 3;
M[i,1] = 'PD' + char(pds[i],4,1);
end; 

print M;
quit;

But surely this makes it difficult to recover the numeric value later on.  So why not have one numeric and one character matrix?

 

View solution in original post

5 REPLIES 5
IanWakeling
Barite | Level 11

The matrix M would have to be defined as a character for this to work.  For example:

 

proc iml;
/* data example */
pds = {0.3, 0.4, 0.8};

M = J(3,1,'       ');
do i=1 to 3;
M[i,1] = 'PD' + char(pds[i],4,1);
end; 

print M;
quit;

But surely this makes it difficult to recover the numeric value later on.  So why not have one numeric and one character matrix?

 

philip_
Fluorite | Level 6

Ian, thank you! This helps a lot, but brings up two related questions:

- is it possible to have a mixed matrix within IML (i.e. one column is numeric, and one character) ?

- do I need to print the blank spaces ('   ') in the initialization of the matrix or is there also a shortcut?

 

(Regarding your suggestion with the two separate matrices: this is just some "weird" flag that is required, the numerical value would still be given in an additional column)

 

Thanks!

Rick_SAS
SAS Super FREQ

As Ian says, please tell us what you are trying to accomplish. The easiest approach might be to have a matrix of "values" and another matrix of "labels",

 

In answer to your question, matrices are either ALL NUMERIC or ALL CHARACTER. However, SAS/IML supports the "Table" data structure, which is like an in-memory version of a data set. A table can contain columns of either data type.  Tables are new in SAS/IML 14.2 (SAS 9.4m4).

IanWakeling
Barite | Level 11

Regarding the other question :

M = J(3,1,BlankStr(6));

would be a way of declaring a character matrix with a specific string length.

Ksharp
Super User
proc iml;
/* data example */
pds = {0.3, 0.4, 0.8};

M = 'PD ' + strip(char(pds));


print M;
quit;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 5 replies
  • 2751 views
  • 8 likes
  • 4 in conversation