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 10-26-2010 09:15 AM
(3152 views)

Hello,

Does anyone know what is the SAS/IML equivalent of the R "sample" function?

Thanks,

Eyal

Does anyone know what is the SAS/IML equivalent of the R "sample" function?

Thanks,

Eyal

4 REPLIES 4

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

Are you sampling with replacement or without? Equal probability or unequal?

For equal-prob sampling without replacement, use permutations. I just did a series of blog posts on generating permutations in IML:

http://blogs.sas.com/iml/index.php?/archives/16-Generating-Random-Permutations.html

For sampling with replacement, I present a a complete module (equal or unequal probabilities) in Chapter 12 of my book*Statistial Programming with SAS/IML Stoftware*. Within the next few weeks, the programs for my book should be available for download at http://support.sas.com/publishing/authors/wicklin.html

Since the code isn't posted there yet, here's some (less general) code for the case of random sampling with replacement under uniform probability:

proc iml;

/** Module for random sampling with replacement and uniform probability.

Assume A is row vector with k elements. **/

start SampleReplaceUni(A, nSamples, nRep);

k = ncol(A); /** number of elements **/

results = j(nSamples, nRep); /** allocate result matrix **/

call randgen(results, "Uniform"); /** fill with random U(0,1) **/

results = ceil(k*results); /** convert to integers 1,2,...k **/

return (shape(A[results], nSamples)); /** reshape and return from A **/

finish;

call randseed(1234);

/** Draw 5 samples from {1,2,...,8}. Do it 10 times. **/

s = SampleReplaceUni(1:8, 10, 5);

print s;

For equal-prob sampling without replacement, use permutations. I just did a series of blog posts on generating permutations in IML:

http://blogs.sas.com/iml/index.php?/archives/16-Generating-Random-Permutations.html

For sampling with replacement, I present a a complete module (equal or unequal probabilities) in Chapter 12 of my book

Since the code isn't posted there yet, here's some (less general) code for the case of random sampling with replacement under uniform probability:

proc iml;

/** Module for random sampling with replacement and uniform probability.

Assume A is row vector with k elements. **/

start SampleReplaceUni(A, nSamples, nRep);

k = ncol(A); /** number of elements **/

results = j(nSamples, nRep); /** allocate result matrix **/

call randgen(results, "Uniform"); /** fill with random U(0,1) **/

results = ceil(k*results); /** convert to integers 1,2,...k **/

return (shape(A[results], nSamples)); /** reshape and return from A **/

finish;

call randseed(1234);

/** Draw 5 samples from {1,2,...,8}. Do it 10 times. **/

s = SampleReplaceUni(1:8, 10, 5);

print s;

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

Hello Rick,

I am sampling with replacement and with unequal probabilities.

Specifically, this is the R statement:

mu <- sample(c(4,12),size=1000,replace=T,prob=c(0.75,0.25))

Is the code you wrote doing this?

Is there no built-in IML (or IMLPlus) function for this?

Thanks,

Eyal

I am sampling with replacement and with unequal probabilities.

Specifically, this is the R statement:

mu <- sample(c(4,12),size=1000,replace=T,prob=c(0.75,0.25))

Is the code you wrote doing this?

Is there no built-in IML (or IMLPlus) function for this?

Thanks,

Eyal

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

The previous module is for equal probability. For unequal prob, use the following:

proc iml;

/** Module for random sampling with replacement and unequal probability.

* Assume A is row vector with k elements and

* PROB is vector of probabilities {p1, p2, ..., pk}. **/

start SampleReplaceUneq(A, nSamples, nRep, prob);

results = j(nSamples, nRep); /** allocate result matrix **/

call randgen(results, "Table", prob); /** fill with 1,2,...k **/

return (shape(A[results], nSamples)); /** reshape and return from A **/

finish;

call randseed(1234);

set = {4 12};

k = ncol(set);

/** Resample from the set with given probabilities. Do it 1000 times. **/

mu = SampleReplaceUneq(set, 1000, k, {0.75 0.25});

proc iml;

/** Module for random sampling with replacement and unequal probability.

* Assume A is row vector with k elements and

* PROB is vector of probabilities {p1, p2, ..., pk}. **/

start SampleReplaceUneq(A, nSamples, nRep, prob);

results = j(nSamples, nRep); /** allocate result matrix **/

call randgen(results, "Table", prob); /** fill with 1,2,...k **/

return (shape(A[results], nSamples)); /** reshape and return from A **/

finish;

call randseed(1234);

set = {4 12};

k = ncol(set);

/** Resample from the set with given probabilities. Do it 1000 times. **/

mu = SampleReplaceUneq(set, 1000, k, {0.75 0.25});

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

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