BookmarkSubscribeRSS Feed
EyalGonen
Lapis Lazuli | Level 10
Hello,

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

Thanks,
Eyal
4 REPLIES 4
Rick_SAS
SAS Super FREQ
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;
EyalGonen
Lapis Lazuli | Level 10
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
Rick_SAS
SAS Super FREQ
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});
Rick_SAS
SAS Super FREQ

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 4 replies
  • 2324 views
  • 0 likes
  • 2 in conversation