## SAS IML: Mapping a function/module to values from a list (like in Mathematica the MAP Function)?

Hello,

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?

In Mathematica you can map functions to a list:

https://reference.wolfram.com/language/ref/Map.html

In our IML case we have to make a loop over cashflows which we want to discount (using an own discFactor_s Function).

`do i = 1 to nrow(cf_m);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*/);end;`

But we would like to apply the cashflow  lines of the matrix to the function discFactor_s without a loop (we would like to avoid loops where ever possible):

cf_d_v = MAP[discFactor_s(discToDate_s, #, forward_m), cf_m)]....

# -> the map function should take each row from the matrix cf_m  and extract the value from second column.

In Mathematica there is # used as represant of the list item taken from the list (in this case cf_m).

Does exist a comfortable solution for that?

Regards,

3 REPLIES 3

## Re: SAS IML: Mapping a function/module to values from a list (like in Mathematica the MAP Function)?

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

cf_d_v = cf_m[, 3] # discFactor_s(discToDate_s, cf_m[, 2], forward_m);

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.

## Re: SAS IML: Mapping a function/module to values from a list (like in Mathematica the MAP Function)?

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)

With pride 🙂 I present a MAP function in SAS IML  using the new SAS IML LISTS object...

``````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#list\$i", list_input);

/*Printing Content of the resulting list*/
call listprint(output);
quit;``````

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.

Any suggestions for improvement very welcome...

## Re: SAS IML: Mapping a function/module to values from a list (like in Mathematica the MAP Function)?

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 "Evaluate a function by using the function name in SAS/IML."

My only substantive comment is that your goal was to perform the computation "without a loop (we would like to avoid loops where ever possible)."  You haven't done that. You've merely put the loop into a module so that it is hidden.

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:

``````/*Define list of values (matrices)*/
input = {1  2,
3  4,
5  6};
output = input[,1] # input[,2];``````

From The DO Loop