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

☑ This topic is **solved**.
Need further help from the community? Please
sign in and ask a **new** question.

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

Posted 08-10-2023 03:20 PM
(931 views)

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.

********************************************************

proc iml;

n=20; m=15; K=J(m,1,0); K[m]=n-m;

L=K; m1=m; m2=m;

Theta1 =2; Theta2=5;

seed=22;

start Uniform_p(m,R) global(seed);

** The Required Progressively Type-II from U(0,1);

W=ranuni( repeat(seed,m,1) );

V=J(m,1,0); U=J(m,1,0);

Do i=1 to m;

Sum=0;

Do j=m-i+1 to m;

Sum=Sum+R[j];

End;

Sum=Sum+i;

V[i]=W[i]**(1/Sum);

End;

Do i=1 to m;

U[i]=1-prod ( V[m: (m-i+1)] );

End;

Call sort (U);

return U;

Finish;

start Adaptive(X,m,R,theta) ;

T=X[CEIL(m/4)];

Do idx=1 to m-1;

If (( X[idx] < T) & (T <= X[idx+1] )) then q=idx;

End;

If X[m]>T then

Do;

W=Uniform_p(m-q,R);

X_m_q = T - theta*log(1-W);

X_Adptive= X[1:q]//X_m_q;

End;

else

Do;

q= m;

X_Adptive=X;

End;

return X_Adptive; * I need to return X_Adptive , q and T;

Finish;

X = - theta1 * log (1- uniform_p(m1,K));

Y = - theta2 * log (1- uniform_p(m2,L));

X_ADP=Adaptive(X,m1,K,theta1);

Y_ADP=Adaptive(Y,m2,L,theta2);

Print q T X X_ADP;

quit;

1 ACCEPTED SOLUTION

Accepted Solutions

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

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.

8 REPLIES 8

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

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

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

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

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

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.

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

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.

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

Thank you so much for your help.

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

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.

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

Hello Rick

Thank you for the suggestions. Is it ok to use

Do i=1 to m;

W[i]=Uniform(seed);

End;

instead of the ranuni which I used earlier in my code?

As for the second suggestion, I am not sure I understood it.

Thank you

**Available on demand!**

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

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.