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.
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.
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.
Perfect, thanks Rick. I made sure to update to 9.4 (12.3) after your recent blog post.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.