Turn on suggestions

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

Showing results for

Options

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 06-12-2019 12:54 PM
(925 views)

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, **#[2]**, forward_m), cf_m)]....

**#[2] -> 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?

Thanks in advance!

Regards,

Adalbert

3 REPLIES 3

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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[1]#list$i[2]", list_input);
/*Printing Content of the resulting list*/
package load ListUtil;
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...

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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];
```

Are you ready for the spotlight? We're accepting content ideas for **SAS Innovate 2025** to be held May 6-9 in Orlando, FL. The call is **open **until September 25. Read more here about **why** you should contribute and **what is in it** for you!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.