## Matrix with strings in IML

Solved
Occasional Contributor
Posts: 7

# 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
Regular Contributor
Posts: 163

## 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?

All Replies
Solution
‎05-18-2017 05:52 AM
Regular Contributor
Posts: 163

## 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

Posted in reply to IanWakeling

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: 4,175

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

Regular Contributor
Posts: 163

## 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: 10,695

## 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
• 357 views
• 8 likes
• 4 in conversation