turn on suggestions

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

Showing results for

Find a Community

Topic Options

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-24-2017 11:15 AM

I have a question that seems really simple but I've struggling a lot with it: How can one perform, in IML, the equivalent of Excel's VLOOKUP without using a loop?

For instance, I'd like to find the index of elements of **a** in **b**, and use that index to return the corresponding element in **c**.

```
proc iml;
a = {104,106,101,104};
b = {101,102,103,104,105,106};
c = {"A", "B","C","D","E","F"};
quit;
```

Expected output is a column matrix containing elements: {"D","F","A","D"}. I can get this result using a loop, but for very large data it slows the IML process to a screeching halt, as loops usually do.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to MDaniel

11-24-2017 06:37 PM - edited 11-25-2017 05:27 AM

You didn't show how you are using the loop, not specify the size of the A and B vectors. In general, the operation you are requesting is of the order N*M where A has N elements and B has M elements because for each element of A you have to search through all elements of B.

If you can treat the vectors as sets in which order doesn't matter, you can use the LOC and ELEMENT functions to obtain the values in B that correspond to elements of A:

```
proc iml;
a = {104,106,101,104};
b = {101,102,103,104,105,106};
c = {"A", "B","C","D","E","F"};
idx = element(b, a);
ans = c[loc(idx)];
print ans; /* answer as a set; order does not matter */
```

However, if you want to preserve order and permit duplicate values, then the following loop is probably the method I'd use:

```
ans = j(nrow(a), 1, " ");
do i = 1 to nrow(a);
ans[i] = c[ loc(a[i] = b) ];
end;
print ans;
```

Another efficient approach would be to sort A and B (and C, sorted by B) and then do a match merge in Base SAS. That would probably be the fastest.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to MDaniel

11-25-2017 01:15 AM - edited 11-25-2017 11:04 AM

If you want to translate 104 to 'D' then use a format.

```
proc format ;
value lookup
101 = 'A'
102 = 'B'
103 = 'C'
104 = 'D'
105 = 'E'
106 = 'F'
;
run;
proc iml;
a = {104,106,101,104};
ans =putn(a,'lookup.');
print ans ;
quit;
```

Result.:

ans D F A D

It is easy to use the CTNLIN= option on PROC FORMAT to generate a format from a dataset.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to MDaniel

11-25-2017 05:05 AM

Yeah. I am also looking for such function in IML. Do loop you mean this?

```
proc iml;
a = {104,106,101,104,102,102,102,103,105,105};
b = {101,102,103,104,105,106};
c = {"A", "B","C","D","E","F"};
want=j(nrow(a),1,' ');
do i=1 to nrow(b);
idx=loc(a=b[i]);
want[idx]=c[i];
end;
print want;
quit;
```