Hi
I am simulating data and calling module function from within another module. My problem is that I need to return the vector of data that I created inside my module along with the index (q).
I kindly ask for your help.
********************************************************
You're on the right track, but you're still returning just the vector X_Adptive from your Adaptive module. When you create the list P1 in that module, you need to include X_Adptive as an element of the list (in addition to T and q) and then return P1:
P1 = [ X_Adptive, T, q];
return P1;
And later on, note that when you call the Adaptive module, you access elements of the returned list like this:
adaptiveRes = Adaptive(X,m1,K,theta1);
X_ADP = adaptiveRes$1;
T = adaptiveRes$2;
q = adaptiveRes$3;
I would also recommend reviewing the use of named lists in the above linked blog post. In the examples above, you can see I'm extracting list items using their positions (i.e., adaptiveRes$1
is the first item of the list). However, giving list items names can improve code readability. For example, in your case, you could name your list items 'X_Adaptive', 't', and 'q', and then extract those by name rather than position.
One option is to return a list. @Rick_SAS wrote about lists here: https://blogs.sas.com/content/iml/2017/03/29/lists-sasiml.html . In your case, you would want to return a list of two items, where the first item is the simulated vector, and the second item is the index value.
Thank you for your suggestions. I read the article and I even went to SAS help to read more about it. However, I am not sure where to put it in my code!!
Inside the subroutine? and How?
Can you please demonstrate into my code.
Thank you
At the end of your Adaptive module, it looks like you want to return X_Adptive, q and t, but you are only returning X_Adptive. To return all three, you can create a list that contains those three items (see here https://blogs.sas.com/content/iml/2018/01/22/iml-lists-syntax.html ) and return the list from the module.
Thank you Nike_N for your help.
I added the list as you suggested, unfortunately the code didn't work and SAS didn't recognize the list. I am not sure if this is due to defining it inside my subroutine or something else. Please advise. The code attached below.
You're on the right track, but you're still returning just the vector X_Adptive from your Adaptive module. When you create the list P1 in that module, you need to include X_Adptive as an element of the list (in addition to T and q) and then return P1:
P1 = [ X_Adptive, T, q];
return P1;
And later on, note that when you call the Adaptive module, you access elements of the returned list like this:
adaptiveRes = Adaptive(X,m1,K,theta1);
X_ADP = adaptiveRes$1;
T = adaptiveRes$2;
q = adaptiveRes$3;
I would also recommend reviewing the use of named lists in the above linked blog post. In the examples above, you can see I'm extracting list items using their positions (i.e., adaptiveRes$1
is the first item of the list). However, giving list items names can improve code readability. For example, in your case, you could name your list items 'X_Adaptive', 't', and 'q', and then extract those by name rather than position.
In addition to what @Mike_N says, I would gently suggest that you consider two changes to your program:
1. Use the RANDGEN function instead of RANUNI. See Six reasons you should stop using the RANUNI function to generate random numbers - The DO Loop (sas....
2. Vectorize the program to achieve better performance. See How to vectorize computations in a matrix language - The DO Loop (sas.com)
I suspect @Mike_N can help with my second suggestion if it is not clear.
Hello Rick
Thank you for the suggestions. Is it ok to use
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!
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.