<?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: SAS IML: Mapping a function/module to values from a list (like in Mathematica the MAP Function)? in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/SAS-IML-Mapping-a-function-module-to-values-from-a-list-like-in/m-p/565643#M4697</link>
    <description>&lt;P&gt;That's fine. As you demonstrate, lists and the CALL EXECUTE subroutine are two powerful tools that provide great flexibility. For a few additional thought about using CALL EXECUTE to implement functions specified by a string, see &lt;A href="https://blogs.sas.com/content/iml/2017/11/01/pass-function-name-sasiml.html" target="_self"&gt;"Evaluate a function by using the function name in SAS/IML."&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My only substantive comment is that your goal was to perform the computation "&lt;SPAN&gt;without a loop (we would like to avoid loops where ever possible)."&amp;nbsp; You haven't done that. You've merely put the loop into a module so that it is hidden.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I realize that your actual computation is more complicated than your example, but if you are seeking efficiency, you can use matrices instead of lists and avoid the loop:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Define list of values (matrices)*/
input = {1  2, 
         3  4, 
         5  6};
output = input[,1] # input[,2];&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 12 Jun 2019 18:36:28 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2019-06-12T18:36:28Z</dc:date>
    <item>
      <title>SAS IML: Mapping a function/module to values from a list (like in Mathematica the MAP Function)?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/SAS-IML-Mapping-a-function-module-to-values-from-a-list-like-in/m-p/565621#M4694</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it seams that I have to build my own a module which replicates functionality of the Mathematica function "MAP". Or maybe somebody has already a solution for that?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In Mathematica you can map functions to a list:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://reference.wolfram.com/language/ref/Map.html" target="_blank" rel="noopener"&gt;https://reference.wolfram.com/language/ref/Map.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In our IML case we have to make a loop over cashflows which we want to discount (using an own discFactor_s Function).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;do i = 1 to nrow(cf_m);&lt;BR /&gt;cf_d_v[i] = cf_m[i, 3] /*CF_AMT*/ # discFactor_s(discToDate_s, cf_m[i, 2] /*CF_DT*/, forward_m /*QUOTE_DT FROM_DT TO_DT QUOTE_RT*/);&lt;BR /&gt;end;&lt;/PRE&gt;&lt;P&gt;But we would like to apply the cashflow&amp;nbsp; lines of the matrix to the function&amp;nbsp;discFactor_s without a loop (we would like to avoid loops where ever possible):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;cf_d_v = MAP[discFactor_s(discToDate_s, &lt;STRONG&gt;#[2]&lt;/STRONG&gt;, forward_m),&amp;nbsp;cf_m)]....&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;#[2] -&amp;gt; the map function should take each row from the matrix cf_m&amp;nbsp; and extract the value from second column.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;In Mathematica there is # used as represant of the list item taken from the list (in this case cf_m).&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does exist a comfortable solution for that?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Adalbert&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 16:54:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/SAS-IML-Mapping-a-function-module-to-values-from-a-list-like-in/m-p/565621#M4694</guid>
      <dc:creator>AThomalla</dc:creator>
      <dc:date>2019-06-12T16:54:11Z</dc:date>
    </item>
    <item>
      <title>Re: SAS IML: Mapping a function/module to values from a list (like in Mathematica the MAP Function)?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/SAS-IML-Mapping-a-function-module-to-values-from-a-list-like-in/m-p/565632#M4695</link>
      <description>&lt;P&gt;If cf_m is a matrix, then implement discFactor_s in a vectorized form so that the second argument can be a column vector and the function returns a column vector. Then the operation you want is&amp;nbsp;&lt;/P&gt;
&lt;P&gt;cf_d_v = cf_m[, 3] # discFactor_s(discToDate_s, cf_m[, 2], forward_m);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that doesn't answer your question, then please post a program that includes sample data, a sample function, and the output that you want.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 17:41:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/SAS-IML-Mapping-a-function-module-to-values-from-a-list-like-in/m-p/565632#M4695</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2019-06-12T17:41:13Z</dc:date>
    </item>
    <item>
      <title>Re: SAS IML: Mapping a function/module to values from a list (like in Mathematica the MAP Function)?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/SAS-IML-Mapping-a-function-module-to-values-from-a-list-like-in/m-p/565637#M4696</link>
      <description>&lt;P&gt;Thanks for the answer and your fast reply. Yes, that would be also a solution. But I thought about a general solution for such problems. Now I have tryed to built one (although it does not directly solves the problem, it goes into the direction of my desired function)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With pride &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; I present a MAP function in SAS IML&amp;nbsp; using the new SAS IML LISTS object...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
start map(f, list);
n_list_l = ListLen(list);
list_out = ListCreate(n_list_l);
do i = 1 to n_list_l;
	call execute("result = " + f + ";");
	call ListSetItem(list_out, i, result);
end;
measure = list_out;
return(measure);
finish map;

/*Define list of values (matrices)*/
list_input = [{1, 2}, {3, 4}, {5, 6}];
/*use new map function to apply specific items of the list*/
output = map("list$i[1]#list$i[2]", list_input);

/*Printing Content of the resulting list*/
package load ListUtil;
call listprint(output);
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If you use the new "map" function you have to use the reserved words "list" and "i" within the first parameter of the function.... "list" refers always to the 2nd parameter of the map function. And "i" is the index running through the list. So in the first parameter you can execute some sort of operation or existing function which is applied or mapped to each entry in the list. The output is again a list of generic items.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions for improvement very welcome...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 18:21:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/SAS-IML-Mapping-a-function-module-to-values-from-a-list-like-in/m-p/565637#M4696</guid>
      <dc:creator>AThomalla</dc:creator>
      <dc:date>2019-06-12T18:21:56Z</dc:date>
    </item>
    <item>
      <title>Re: SAS IML: Mapping a function/module to values from a list (like in Mathematica the MAP Function)?</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/SAS-IML-Mapping-a-function-module-to-values-from-a-list-like-in/m-p/565643#M4697</link>
      <description>&lt;P&gt;That's fine. As you demonstrate, lists and the CALL EXECUTE subroutine are two powerful tools that provide great flexibility. For a few additional thought about using CALL EXECUTE to implement functions specified by a string, see &lt;A href="https://blogs.sas.com/content/iml/2017/11/01/pass-function-name-sasiml.html" target="_self"&gt;"Evaluate a function by using the function name in SAS/IML."&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My only substantive comment is that your goal was to perform the computation "&lt;SPAN&gt;without a loop (we would like to avoid loops where ever possible)."&amp;nbsp; You haven't done that. You've merely put the loop into a module so that it is hidden.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I realize that your actual computation is more complicated than your example, but if you are seeking efficiency, you can use matrices instead of lists and avoid the loop:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Define list of values (matrices)*/
input = {1  2, 
         3  4, 
         5  6};
output = input[,1] # input[,2];&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jun 2019 18:36:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/SAS-IML-Mapping-a-function-module-to-values-from-a-list-like-in/m-p/565643#M4697</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2019-06-12T18:36:28Z</dc:date>
    </item>
  </channel>
</rss>

