Desktop productivity for business analysts and programmers

Non_Duplicating Random Number Assignment (Best Practice)

Reply
Contributor
Posts: 49

Non_Duplicating Random Number Assignment (Best Practice)

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

Super User
Posts: 11,114

Re: Non_Duplicating Random Number Assignment (Best Practice)

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.

Super User
Posts: 5,361

Re: Non_Duplicating Random Number Assignment (Best Practice)

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.

Ask a Question
Discussion stats
  • 2 replies
  • 219 views
  • 3 likes
  • 3 in conversation