开发者

Equivalent to R findInterval() function in SAS IML

Is there anything similar to R's findInterval (or cut) in SAS, specifically in IML?

I'm converting an R program of mine that does Monte Carlo simulations to IML, and it uses findInterval to convert the numbers from the random number generator to an output state. I can write something in IML to replace it, but it's t开发者_运维知识库erribly slow compared to the original. This is because findInterval takes advantage of compiled C code; is there anything similar that I can use in SAS?


Are your breaks uniform (equal probability) or not? For uniform breaks, you can use ceil(k*u) where u is the vector or random uniform numbers. For example, if you want 10 observations randomly assigned to the numbers 1-4 with equal probability, you can say

y = ceil(4*ranuni(j(10,1)));

or, if you want to use the newer random number generator,

u = j(10,1); /** allocate **/
call randgen(u, "uniform"); /** fill with U[0,1] **/
y = ceil(4*u);

For unequal probability, use the "table" distribution. For example,

p = {0.1 0.5 0.2 0.2}; /** four categories with given probabilities **/
y = j(10, 1); 
call randgen(y, "Table", p); /** fills with 1-4 with probability p **/

You might be interested in using the SampleWithReplace module from Chapter 13 of my book, Statistical Programming with SAS/IML software. You can download the code and see an example of its use at http://blogs.sas.com/iml/index.php?/archives/75-Hey!-Those-Two-People-Have-the-Same-Initials!.html

Both of these techniques eliminate the need for findInterval because they produce the categories directly. If you REALLY REALLY think you need to bin the random numbers, you can use the algorithm I describe here: http://blogs.sas.com/iml/index.php?/archives/80-Count-the-Number-of-Points-in-Bins-Efficiently.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜