Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 08-03-2017 08:43 AM
(1426 views)

I need to look-up the values from matrix 'a' in the first column of matrix 'b' and return the values in the *second* column of matrix 'b' to a new matrix named 'c'. The rows in matrix 'b' are a key-value pairs, basically.

This code achieves that, but I would like to know whether there is a vectorized aproach to this instead of a do loop.

```
proc iml;
a = {100 202 303,
107 200 303,
101 205 302,
102 200 300};
b = {100 1,
101 8,
102 1,
104 6,
107 4,
108 9,
200 4,
202 6,
205 2,
300 10,
301 5,
302 2,
303 8,
304 10};
/*the Do LOOP way*/
c = j(4,3,.);
do i = 1 to 4;
do j = 1 to 3;
idx = loc(b[,1] = a[i,j]);
c[i,j] = b[idx,2];
end;
end;
print c;
quit;
```

The expected output in matrix 'c':

c 1 6 8 4 4 8 8 2 2 1 4 10

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Ian deduced the problem correctly. I assumed that the number of elements that match was always NCOL(a).. In your random data, that assumption does not hold. If you need to handle the case where there are fewer elements that match than there are columns of a, use the following:

```
do i = 1 to nrow(a);
idx = loc( element(b[,1], a[i,]) ); /* location of elements in b[,1] that match a[i,] */
if ncol(idx)>0 then
c[i,1:ncol(idx)] = T( b[idx,2] ); /* copy elements from b[,2] */
end;
```

If you can have MORE elements that match than columns of a, then you either need to truncate the results to NCOL(a) elements of make some other modification.

5 REPLIES 5

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Yes. You want to use the ELEMENT function to find the vector of locations in b[,1] that match the elements in a[i,] for each row of a.

You can read about the LOC-ELEMENT technique in the article "Finding observations that satisfy multiple conditions: The LOC-ELEMENT technique"

```
c = j(nrow(a),ncol(a),.);
do i = 1 to nrow(a);
idx = loc( element(b[,1], a[i,]) ); /* location of elements in b[,1] that match a[i,] */
c[i,] = T( b[idx,2] ); /* copy elements from b[,2] */
end;
print c;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thank you for you answer, Rick.

I am trying to extend and test your solution for a larger number of rows and columns for matrix 'a' and I ran into a problem. For 100 rows nad 5 columns, I receive this error: **ERROR: (execution) Matrices do not conform to the operation.**

Please see below for code that generates some random data - I'm still not fully familiar with ELEMENT function, but I can't find anything wrong with the code.

```
proc iml;
rows = 100;
cols = 5;
min = 100;
max = 1000;
call randseed(555);
a = j(rows,cols,.);
call randgen(a, "Uniform"); /* ~ U(0,1) */
a = min + floor( (1+Max-Min)*a );
x1 = min:max;
x1 = shape(x1,max-min+1,1);
x2 = j(max-min+1,1,.);
call randgen(x2, "Uniform"); /* ~ U(0,1) */
x2 = 10 + floor( (1+50-10)*x2 );
b = x1||x2;
/*vectorization*/
c = j(nrow(a),ncol(a),.);
do i = 1 to nrow(a);
idx = loc( element(b[,1], a[i,]) ); /* location of elements in b[,1] that match a[i,] */
c[i,] = T( b[idx,2] ); /* copy elements from b[,2] */
end;
quit;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Ian deduced the problem correctly. I assumed that the number of elements that match was always NCOL(a).. In your random data, that assumption does not hold. If you need to handle the case where there are fewer elements that match than there are columns of a, use the following:

```
do i = 1 to nrow(a);
idx = loc( element(b[,1], a[i,]) ); /* location of elements in b[,1] that match a[i,] */
if ncol(idx)>0 then
c[i,1:ncol(idx)] = T( b[idx,2] ); /* copy elements from b[,2] */
end;
```

If you can have MORE elements that match than columns of a, then you either need to truncate the results to NCOL(a) elements of make some other modification.

Secure your spot at the must-attend AI and analytics event of 2024: SAS Innovate 2024! Get ready for a jam-packed agenda featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events.

Register by March 1 to snag the Early Bird rate of just $695! Don't miss out on this exclusive offer.

** **

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.