BookmarkSubscribeRSS Feed
AThomalla
Obsidian | Level 7

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
Rick_SAS
SAS Super FREQ

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.

AThomalla
Obsidian | Level 7

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...

 

Rick_SAS
SAS Super FREQ

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

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 3 replies
  • 733 views
  • 0 likes
  • 2 in conversation