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 05-09-2014 01:05 AM
(1196 views)

proc iml;

call randseed(4545); * initialize the stream (like streaminit);

x = J(5000,1,.); * pre-allocate space for random numbers;

call randgen(x,'normal',0,1); * fill x with N(0,1) deviates;

y = y + (x**2 - 3*x**3 + 5x < 1);

p = y / 5000; * MEAN acts on each matrix column;

se = sqrt(y*(1-y)/5000); * VAR, but not STD or STDERR, also acts on columns;

print "IML STEP: estimated mean of sqrt(abs(X)) is" p "with standard error" se; * use PRINT, not PUT;

I'm trying to use monte carlo integration with proc iml to estimate the probability that x**2 - 3*x**3 + 5x is less than 1. What am I doing wrong? Do loops are not allowed by the way.

proc iml;

start tr(x,y); * create function called tr;

/* handle bad case */

/*if x<0 then do; return (.); end; * send back missing;*/

N = nrow(x);

dx = x[2:N] - x[1:N-1];

ymean = (y[2:N] + y[1:N-1]) / 2;

return(dx` * ymean );

finish tr;

x = do(-2,5,0.01);

print "Integral of y over x is" ( tr(x,sin(x##2)) );

I keep receiving the (execution) invalid subscript or subscript out of range. How do I solve this problem?

3 REPLIES 3

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

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

As Ian notes, when dealing with a matrix language it is essential to always know the dimensions of your vectors and matrices.

This is a good exercise. Your professor has constructed two classic problems. For the Monte Carlo problem, look carefully at the statement

y = y + (f(x)<1));

Has y been defined? If not, you cannot use it on the right hand side of an expression.

Good luck!

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

You should also look up the difference between the ** operator and the ## operator. Here is the doc:

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.