Hi All
I am hoping someone has come across this issue previously and knows why it happens: I am assigning a random number between 1 and 1,000 to a daily feed of data by making use of the RANUNI function:
SAS_RANDOM_NUMBER = int(1000*ranuni(-1));
The problem I have is that on certain days the same number is allocated to 90% of the records, so not so random after all. Has anyone ever come across this problem?
Try using the Rand Function instead.
Rick Wicklin explains why in the article Six reasons you should stop using the RANUNI function to generate random numbers.
Don't use RANUNI, use the improved RAND:
call streaminit(-1);
SAS_RANDOM_NUMBER = int(1000*rand('uniform'));
If you want a uniform integer distribution, use
rand('integer',1000);
Thanks guys! Will that explain why more than 90% of the records get the same number assigned though? Is RANUNI that "bad"?
Well, I never experienced something as egregious as what you describe, but then I rarely needed random numbers. I do mostly ETL work, no simulations.
And I've been using RAND since it was introduced.
It looks like you're specifying a seed value of -1, which uses the time of day to initialize the seed stream. I don't know how you're using the RANUNI function to generate the list of random numbers, but if (?) you're including the function in a loop, it's possible that this runs faster than the clock updates -- and therefore reuses the same seed.
For more information, see How to Generate Random Numbers in SAS by @Rick_SAS .
If you post the code you are using, we can say more about what you are seeing and why.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.