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

Showing results for

Find a Community

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

08-14-2013 03:04 PM

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.

Accepted Solutions

Solution

08-14-2013
03:11 PM

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

Posted in reply to stoffprof

08-14-2013 03:11 PM

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.

All Replies

Solution

08-14-2013
03:11 PM

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

Posted in reply to stoffprof

08-14-2013 03:11 PM

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.

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

Posted in reply to Rick_SAS

08-14-2013 03:21 PM

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