BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
chrissowden
Obsidian | Level 7

Capture.PNG

 

I am trying to assign random numbers 1-18 as a new variable to my dataset.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
Later versions of SAS support INTEGER as a RAND() option.

X = rand(‘integer’, 1, 18);

If your version doesn’t support this (check under the RAND() function), you can use a modification using the RANUNI() function.

View solution in original post

5 REPLIES 5
Patrick
Opal | Level 21

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;

 

Reeza
Super User
Later versions of SAS support INTEGER as a RAND() option.

X = rand(‘integer’, 1, 18);

If your version doesn’t support this (check under the RAND() function), you can use a modification using the RANUNI() function.
novinosrin
Tourmaline | Level 20

Never knew the integer option in rand function. Didn't see it in doc either. Nice. Does it pick on a uniform distribution?

FreelanceReinh
Jade | Level 19

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.

 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 13993 views
  • 4 likes
  • 5 in conversation