<?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: Similar to LOC, but want index of nearest lesser value in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Similar-to-LOC-but-want-index-of-nearest-lesser-value/m-p/807460#M5804</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;so the original requirement shifted slightly, wondering if it can be a slight mod to this.&amp;nbsp; I am taking these index values and using them to find the value in another vector at those indecies.&amp;nbsp; &amp;nbsp;Now, the requirement is if there isn't an exact match, use the higher and lower value in the other vector.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So for example, since 10 and 19 are not exact matches, I would average the 3rd and 4th values in the other vector.&amp;nbsp; &amp;nbsp;Can you please help with that?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 12 Apr 2022 18:50:58 GMT</pubDate>
    <dc:creator>nicholasbromley</dc:creator>
    <dc:date>2022-04-12T18:50:58Z</dc:date>
    <item>
      <title>Similar to LOC, but want index of nearest lesser value</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Similar-to-LOC-but-want-index-of-nearest-lesser-value/m-p/804791#M5793</link>
      <description>&lt;P&gt;I am comparing arrays and need to take values from array A and find the index of the matching value or next lower value in array B.&amp;nbsp; B is ordered if that helps the solution.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So using LOC would give the the index of the exact match, but if there isn't an exact match, I want the next lower value.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A = {10, 3, 19}&amp;nbsp;&lt;/P&gt;&lt;P&gt;B= {1, 3, 7, 42}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Wanted Result = {3, 2, 3}&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2022 15:23:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Similar-to-LOC-but-want-index-of-nearest-lesser-value/m-p/804791#M5793</guid>
      <dc:creator>nicholasbromley</dc:creator>
      <dc:date>2022-03-29T15:23:28Z</dc:date>
    </item>
    <item>
      <title>Re: Similar to LOC, but want index of nearest lesser value</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Similar-to-LOC-but-want-index-of-nearest-lesser-value/m-p/804797#M5794</link>
      <description>&lt;P&gt;OK, then you want the largest index of B for which B &amp;lt;= A[i] for each element of A.&lt;/P&gt;
&lt;P&gt;This might be empty, so you also need to handle that case:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
A = {10, 3, 19, 0};
B= {1, 3, 7, 42};  /* assume B is sorted in increasing order */

Result = j(nrow(A), 1, .);
do i = 1 to nrow(A);
   idx = loc(B &amp;lt;= A[i]);
   if ncol(idx) &amp;gt; 0 then            /* did we find any? */
      Result[i] = idx[ ncol(idx) ]; /* keep the largest */
end;

print A Result;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Mar 2022 15:39:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Similar-to-LOC-but-want-index-of-nearest-lesser-value/m-p/804797#M5794</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-03-29T15:39:27Z</dc:date>
    </item>
    <item>
      <title>Re: Similar to LOC, but want index of nearest lesser value</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Similar-to-LOC-but-want-index-of-nearest-lesser-value/m-p/804811#M5795</link>
      <description>&lt;P&gt;I think you can avoid the need to check for an empty result if you vectorize it as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;A = {10, 3, 19} ;
B= {1, 3, 7, 42};
result = (t(B) &amp;lt;= repeat(A,1,nrow(B)))[,+];
print result;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Mar 2022 16:26:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Similar-to-LOC-but-want-index-of-nearest-lesser-value/m-p/804811#M5795</guid>
      <dc:creator>IanWakeling</dc:creator>
      <dc:date>2022-03-29T16:26:18Z</dc:date>
    </item>
    <item>
      <title>Re: Similar to LOC, but want index of nearest lesser value</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Similar-to-LOC-but-want-index-of-nearest-lesser-value/m-p/804812#M5796</link>
      <description>&lt;P&gt;Yes and no. Since 0 is not a valid index, you still have to check if you intend to use these values to index into the B vector.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I thought about vectorization. It requires more memory instead of the loop, so it really depends on the size of these vectors. Here's the code that I wrote but did not post:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* replace with loops with matrix computations (requires more memory */
MA = repeat(A`, nrow(B), 1);  /* j_th column is A[j] */
MB = repeat(B, 1, nrow(A));   /* i_th row is B[i] */
L = (MB &amp;lt;= MA);               /* 1 iff B[i] &amp;lt;= A[j] */
print L;
Result = L[+,];               /* sum the elements for which B[i] &amp;lt;= A[j] */
print Result;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Mar 2022 16:40:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Similar-to-LOC-but-want-index-of-nearest-lesser-value/m-p/804812#M5796</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-03-29T16:40:58Z</dc:date>
    </item>
    <item>
      <title>Re: Similar to LOC, but want index of nearest lesser value</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Similar-to-LOC-but-want-index-of-nearest-lesser-value/m-p/804882#M5797</link>
      <description>&lt;P&gt;Thank you! Hours spent on this, but alot of learning...&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2022 19:53:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Similar-to-LOC-but-want-index-of-nearest-lesser-value/m-p/804882#M5797</guid>
      <dc:creator>nicholasbromley</dc:creator>
      <dc:date>2022-03-29T19:53:17Z</dc:date>
    </item>
    <item>
      <title>Re: Similar to LOC, but want index of nearest lesser value</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Similar-to-LOC-but-want-index-of-nearest-lesser-value/m-p/804979#M5798</link>
      <description>&lt;P&gt;Indeed size matters, so you need to be aware that nrow(A)*nrow(B) is not too large if vectorization is to be used.&amp;nbsp; I compared&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;result = (t(B) &amp;lt;= repeat(A,1,nrow(B)))[,+];&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;result = (repeat(t(B),nrow(A),1) &amp;lt;= repeat(A,1,nrow(B)))[,+];&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;using FULLSTIMER and I am seeing that the latter uses about 50% more memory for a moderately large problem (10000 x 32), so there is some saving comparing vector to matrix, over matrix to matrix.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Mar 2022 08:06:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Similar-to-LOC-but-want-index-of-nearest-lesser-value/m-p/804979#M5798</guid>
      <dc:creator>IanWakeling</dc:creator>
      <dc:date>2022-03-30T08:06:38Z</dc:date>
    </item>
    <item>
      <title>Re: Similar to LOC, but want index of nearest lesser value</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Similar-to-LOC-but-want-index-of-nearest-lesser-value/m-p/807460#M5804</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;so the original requirement shifted slightly, wondering if it can be a slight mod to this.&amp;nbsp; I am taking these index values and using them to find the value in another vector at those indecies.&amp;nbsp; &amp;nbsp;Now, the requirement is if there isn't an exact match, use the higher and lower value in the other vector.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So for example, since 10 and 19 are not exact matches, I would average the 3rd and 4th values in the other vector.&amp;nbsp; &amp;nbsp;Can you please help with that?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Apr 2022 18:50:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Similar-to-LOC-but-want-index-of-nearest-lesser-value/m-p/807460#M5804</guid>
      <dc:creator>nicholasbromley</dc:creator>
      <dc:date>2022-04-12T18:50:58Z</dc:date>
    </item>
    <item>
      <title>Re: Similar to LOC, but want index of nearest lesser value</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Similar-to-LOC-but-want-index-of-nearest-lesser-value/m-p/807470#M5805</link>
      <description>&lt;P&gt;I suspect you can solve the new problem if you give it some effort.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are two "edge cases" that you need to worry about because there are no "adjacent values" to average:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;What happens if an element of A is less than all elements of B? Is the result missing? Or maybe the 1st element of the Other vector?&lt;/LI&gt;
&lt;LI&gt;What happens if an element of A is greater all elements of B? Is the result missing? Or maybe the last element of the Other vector?&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;In the program below, I've coded one way to handle these edge cases, but you might want to ask your client what they prefer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
A = {10, 3, 19, 0, 45};
B= {1, 3, 7, 42};  /* assume B is sorted in increasing order */
Other = {100, 200, 300, 400, 500};  /* assume Other is same size as B */
nB = nrow(B);
nA = nrow(A);

Result = j(nA, 1, .);
do i = 1 to nA;
   idx = loc(B = A[i]);
   if ncol(idx) &amp;gt; 0 then do;     /* did we find an exact match? */
      Result[i] = Other[idx];    /* if so, index into the Other vec */
   end;
   else do;                      /* not an exact match; average adjacent vals */
      idx = loc(B &amp;lt;= A[i]);
      /* 3 cases: EMPTY, the last index, or valid index that is not last */
      if ncol(idx)=0 then  /* A[i] &amp;lt; all elements of B */
         Result[i] = .;    /* or you could use Other[1] instead */
      else do;
         if all(idx &amp;lt; nB) then do; /* average the Other elements */
            j = idx[ ncol(idx) ];
            Result[i] = (Other[j] + Other[j+1]) / 2;
         end;
         else 
            Result[i] = Other[nB]; /* or missing? */
      end;
   end;
end;

print A Result;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Apr 2022 19:26:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Similar-to-LOC-but-want-index-of-nearest-lesser-value/m-p/807470#M5805</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-04-12T19:26:12Z</dc:date>
    </item>
  </channel>
</rss>

