Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating random numbers in SAS

Tags:

sas

how to generate random integers between 10 and 50 using ranuni() function in sas.

For generating random integer between 1 to 5, I used this code

data random;
 do i=1 to 10;
    random_int = int(ranuni(0) * 5 + 1);
    output;
 end;

run;

like image 268
Sanyam Gupta Avatar asked Jan 02 '13 11:01

Sanyam Gupta


People also ask

How do you generate random values in SAS?

You can generate a set of random numbers in SAS that are uniformly distributed by using the RAND function in the DATA step or by using the RANDGEN subroutine in SAS/IML software. (These same functions also generate random numbers from other common distributions such as binomial and normal.)

How do you generate a random number between 0 and 1 in SAS?

The most common functions to generate random numbers from are the uniform and the normal distribution. This is done with the RAND() function inside a data step, specifying a distribution, and parameters if necessary. RAND('UNIFORM') will provide a random value from 0 to 1.

How do you create a random variable in SAS write a SAS code to generate a single random variable in SAS between 1 and 10?

We simply use the 'Integer' argument and specify the min and max ranges like this. data _null_; r = rand("integer", 1, 10); /* r = rand("integer", 10); equivalent */ put r; run; data _null_; r = rand("integer", 1, 10); /* r = rand("integer", 10); equivalent */ put r; run; Before SAS 9.4M5, we have to do something else.

What is random seed in SAS?

A seed value specifies a particular stream from a set of possible random number streams. When you specify a seed, SAS generates the same set of pseudorandom numbers every time you run the program.


2 Answers

Using the same logic, you could use:

random_int = int(ranuni(0) * 41 + 10);

ranuni returns a value from interval <0, 1>, multiply it by 41 and you get a value from <0, 41>, add 10 and the final value is from <10, 51>. Casting to int just cuts off the decimal part so you will never get 51.

Generaly:

a * ranuni(seed) + b;

returns a value from interval <b, a + b>.

like image 160
Dejan Peretin Avatar answered Oct 16 '22 03:10

Dejan Peretin


If u is a random uniform variate in [0,1], then x = a + (b-a)*u is random uniform on [a,b]. See http://blogs.sas.com/content/iml/2011/08/24/how-to-generate-random-numbers-in-sas/

like image 21
Rick Avatar answered Oct 16 '22 01:10

Rick