<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Proc IML - Equivalent of VLOOKUP (without using loops) in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Proc-IML-Equivalent-of-VLOOKUP-without-using-loops/m-p/416105#M3834</link>
    <description>&lt;P&gt;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&amp;nbsp;B has M elements because for each element of A you have to search through all elements of B.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you can treat the vectors as sets in which order&amp;nbsp;doesn't matter, you can &lt;A href="https://blogs.sas.com/content/iml/2015/05/11/loc-element-trick.html" target="_self"&gt;use the LOC and ELEMENT functions&lt;/A&gt; to obtain the values in B that correspond to elements of A:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 */
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, if you want to preserve order and permit duplicate values, then the following loop is probably the method I'd use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ans = j(nrow(a), 1, " ");
do i = 1 to nrow(a);
   ans[i] = c[ loc(a[i] = b) ];
end;
print ans;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
    <pubDate>Sat, 25 Nov 2017 10:27:32 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2017-11-25T10:27:32Z</dc:date>
    <item>
      <title>Proc IML - Equivalent of VLOOKUP (without using loops)</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Proc-IML-Equivalent-of-VLOOKUP-without-using-loops/m-p/416055#M3833</link>
      <description>&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;For instance, I'd like to find the index of elements of &lt;STRONG&gt;a&lt;/STRONG&gt; in &lt;STRONG&gt;b&lt;/STRONG&gt;, and use that index to return&amp;nbsp;the corresponding element in&amp;nbsp;&lt;STRONG&gt;c&lt;/STRONG&gt;.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
	a = {104,106,101,104};
	b = {101,102,103,104,105,106};
	c = {"A", "B","C","D","E","F"};
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Nov 2017 16:15:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Proc-IML-Equivalent-of-VLOOKUP-without-using-loops/m-p/416055#M3833</guid>
      <dc:creator>MDaniel</dc:creator>
      <dc:date>2017-11-24T16:15:24Z</dc:date>
    </item>
    <item>
      <title>Re: Proc IML - Equivalent of VLOOKUP (without using loops)</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Proc-IML-Equivalent-of-VLOOKUP-without-using-loops/m-p/416105#M3834</link>
      <description>&lt;P&gt;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&amp;nbsp;B has M elements because for each element of A you have to search through all elements of B.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you can treat the vectors as sets in which order&amp;nbsp;doesn't matter, you can &lt;A href="https://blogs.sas.com/content/iml/2015/05/11/loc-element-trick.html" target="_self"&gt;use the LOC and ELEMENT functions&lt;/A&gt; to obtain the values in B that correspond to elements of A:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 */
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, if you want to preserve order and permit duplicate values, then the following loop is probably the method I'd use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ans = j(nrow(a), 1, " ");
do i = 1 to nrow(a);
   ans[i] = c[ loc(a[i] = b) ];
end;
print ans;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Sat, 25 Nov 2017 10:27:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Proc-IML-Equivalent-of-VLOOKUP-without-using-loops/m-p/416105#M3834</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-11-25T10:27:32Z</dc:date>
    </item>
    <item>
      <title>Re: Proc IML - Equivalent of VLOOKUP (without using loops)</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Proc-IML-Equivalent-of-VLOOKUP-without-using-loops/m-p/416117#M3835</link>
      <description>&lt;P&gt;If you want to translate 104 to 'D' then use a format.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Result.:&lt;/P&gt;
&lt;PRE&gt;ans

D
F
A
D&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;It is easy to use the CTNLIN= option on PROC FORMAT to generate a format from a dataset.&lt;/P&gt;</description>
      <pubDate>Sat, 25 Nov 2017 16:04:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Proc-IML-Equivalent-of-VLOOKUP-without-using-loops/m-p/416117#M3835</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-11-25T16:04:50Z</dc:date>
    </item>
    <item>
      <title>Re: Proc IML - Equivalent of VLOOKUP (without using loops)</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Proc-IML-Equivalent-of-VLOOKUP-without-using-loops/m-p/416135#M3836</link>
      <description>&lt;P&gt;Yeah. I am also looking for such function in&amp;nbsp; IML. Do loop you mean this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 25 Nov 2017 10:05:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Proc-IML-Equivalent-of-VLOOKUP-without-using-loops/m-p/416135#M3836</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-11-25T10:05:01Z</dc:date>
    </item>
  </channel>
</rss>

