How to use the RANUNI Function in SAS (With Examples)

The RANUNI function in SAS is used to generate random numbers from a uniform distribution. It returns a random number between 0 and 1. This function can be used for randomizing data, creating random samples, and for simulation studies. Examples of how to use the RANUNI function include generating a random number for each observation in a dataset and creating random samples from a population.


You can use the RANUNI function in SAS to generate values from the .

This function uses the following syntax:

RANUNI(seed)

where:

  • seed: A non-negative integer to use as initial starting point for generating random values.

The following example shows how to use this function in practice.

Example 1: Use RANUNI Function to Generate One Random Value

We can use the following syntax with the RANUNI function to create a dataset that contains one random value between 0 and 1:

/*create dataset with one random value between 0 and 1*/
data my_data;
    my_value=ranuni(0);
run;

/*view dataset*/
proc print data=my_data;

The RANUNI function generated the value 0.49370.

By default, the RANUNI function generates a random value between 0 and 1.

However, you can multiply the result of the RANUNI function by n to instead generate a random value between 1 and n.

For example, we can use the following syntax to generate a random value between 0 and 10:

/*create dataset with one random value between 0 and 10*/
data my_data;
    my_value=ranuni(0)*10;
run;

/*view dataset*/
proc print data=my_data;

This time the RANUNI function generated the value 4.17403.

Example 2: Use RANUNI Function to Generate Several Random Values

We can use the following syntax with the RANUNI function to create a dataset that contains ten random values between 0 and 100:

/*create dataset with 10 random values between 0 and 100*/
data my_data;
    do i=1 to 10 by 1;
        my_value=ranuni(0)*100;
        output;
    end;
run;

/*view dataset*/
proc print data=my_data;

Notice that each of the values in the my_value column are between 0 and 100.

The following tutorials explain how to perform other common tasks in SAS:

x