BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
stoffprof
Fluorite | Level 6

I need to randomly choose k integers between 1 and T without replacement.

Here's what I've come up with:

T=60;

k=15;

/* random vector for choosing integers */

r = j(T,1,0);

call randgen(r,'uniform');


/* find indices from sorting r */

call sortndx( integers, r, {1});


/* take first k integers */

draw = integers[1:k];

I need to do it many times, and I'd rather avoid looping through this, so I'm wondering if anyone has a better solution.

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

There are many ways. Do you have SAS/IML 12.1 (SAS 9.3m2)? If so, use

s = sample(1:60, {15 100}, "WOR");

The sample space is 1:60.  The sample size is 15.  The number of independent draws is 100. The samplnig is done without replacement.

View solution in original post

2 REPLIES 2
Rick_SAS
SAS Super FREQ

There are many ways. Do you have SAS/IML 12.1 (SAS 9.3m2)? If so, use

s = sample(1:60, {15 100}, "WOR");

The sample space is 1:60.  The sample size is 15.  The number of independent draws is 100. The samplnig is done without replacement.

stoffprof
Fluorite | Level 6

Perfect, thanks Rick. I made sure to update to 9.4 (12.3) after your recent blog post.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 2 replies
  • 1547 views
  • 1 like
  • 2 in conversation