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


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

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.

Additional Resources

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

How to Generate Random Numbers in SAS
How to Select a Random Sample in SAS
How to Extract Numbers from String in SAS

Leave a Reply

Your email address will not be published. Required fields are marked *