I am trying to assign random numbers 1-18 as a new variable to my dataset.
The random functions create a value between 0 and 1. If you want values between 1 - 18 then just multiply the value by 18 and then use the ceil() function to shift the result to the next higher integer. Below a code sample using ranuni().
data test;
do i=1 to 100;
ran_num=ceil(ranuni(1)*18);
output;
end;
stop;
run;
Never knew the integer option in rand function. Didn't see it in doc either. Nice. Does it pick on a uniform distribution?
The "Integer" distribution has been introduced in SAS 9.4M5 (according to the last comment in https://blogs.sas.com/content/iml/2015/10/05/random-integers-sas.html). Yes, it creates random values "from the discrete uniform distribution on a finite set of integers." (documentation)
For arbitrary random numbers in a range [a,b] there are new optional arguments a, b in RAND('UNIFORM',a,b). They were available, but not yet documented in release 9.4M2.
Merci !
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.