BookmarkSubscribeRSS Feed
GalacticAbacus
Obsidian | Level 7

Hello,

 

I've cruised this site for multiple methods of random number generation and assignment but did not find a good fit (as far as I know).

My use case is a to assign a random number to a population of individuals. I don't care about uniform distribution of the random values.

Is there a solid way to ensure no duplication of random numbers generated without having to go through do-loop iterations?

 

I realize there is a much higher chance of random number duplication than might otherwise be expected but I'm suspecting there must be a quick/efficient/best-practice out there for this...

 

Any help would be appreciated

 

TS

2 REPLIES 2
ballardw
Super User

It may be appropriate to discuss how you will use that random number. If the purpose is to select a random sample then the procedure Surveyselect may be a better idea.

 

The likelihood of duplication arises with larger numbers of records. So how big is your data set? Also sometimes "duplication" is the result of a default display format rounding values to a number of digits.

Astounding
PROC Star

This may not fit your idea of "quick" or "efficient" but it is tried and true and relatively easy to understand.  Old style coding:

 

data almost_there;

set have;

random_order = ranuni(12345);

run;

 

proc sort data=almost_there;

by random_order;

run;

 

data want;

set almost_there;

random_number = _n_;

drop random_order;

run;

 

Assign a random fraction to each observation.  Then just number the observations in order.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1431 views
  • 3 likes
  • 3 in conversation