Statistical programming, matrix languages, and more

Matrix with strings in IML

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Matrix with strings in IML

[ Edited ]

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!


Accepted Solutions
Solution
‎05-18-2017 05:52 AM
Frequent Contributor
Posts: 146

Re: Matrix with strings in IML

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


All Replies
Solution
‎05-18-2017 05:52 AM
Frequent Contributor
Posts: 146

Re: Matrix with strings in IML

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?

 

Occasional Contributor
Posts: 7

Re: Matrix with strings in IML

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!

SAS Super FREQ
Posts: 3,637

Re: Matrix with strings in IML

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).

Frequent Contributor
Posts: 146

Re: Matrix with strings in IML

Regarding the other question :

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

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

Super User
Posts: 9,878

Re: Matrix with strings in IML

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

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


print M;
quit;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 243 views
  • 8 likes
  • 4 in conversation