turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

10-26-2010 09:15 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to EyalGonen

10-26-2010 10:52 AM

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
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to EyalGonen

10-27-2010 05:32 AM

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
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to EyalGonen

10-27-2010 08:34 AM

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
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to EyalGonen

08-15-2013 09:31 AM

Update: SAS/IML 12.1 (SAS 9.3m2) supports the SAMPLE function, which supports drawing samples with or without replacement from finite sets, with equal or unequal probabilities.