How can I generate random numbers in SAS? 2

How can I generate random numbers in SAS?

Generating random numbers in SAS is a simple process that can be achieved using the RAND function. This function allows the user to generate a sequence of random numbers between 0 and 1, which can then be manipulated to fit specific requirements. The RAND function can also be used in conjunction with other SAS functions, such as ROUND or INT, to generate random integers or decimals within a desired range. Furthermore, SAS also offers a variety of other random number generation functions, such as RANUNI and RANNOR, which allow for more control over the distribution and characteristics of the generated numbers. By utilizing these functions, users can easily generate random numbers in SAS for various statistical and analytical purposes.

Generate Random Numbers in SAS (3 Examples)


You can use the function in SAS to generate random numbers.

The following examples show how to use this function in practice.

Example 1: Generate One Random Number

The following code shows how to generate a single random integer in SAS between 1 and 10:

/*create dataset with variable that contain random value*/
data my_data;
   call streaminit(1);  /*make this example reproducible*/
   x = rand("integer", 1, 10);
   output;
run;

/*view dataset*/
proc printdata=my_data;

The random number between 1 and 10 turned out to be 9.

Note that we used the streaminit() function to ensure that this example is reproducible. This means that each time we run this code, the random number will be 9.

Feel free to leave out the streaminit() function to produce a different random value each time you run the code.

Example 2: Generate Variable with Several Random Numbers

The following code shows how to generate a variable in SAS that contains 10 random values between 1 and 20:

/*create dataset with variable that contain random value*/
data my_data;
   call streaminit(10);
   do i = 1 to 10;
   x = rand("integer", 1, 20);
   output;
   end;
run;

/*view dataset*/
proc printdata=my_data;

Notice that each of the values for the variable x are random integers between 1 and 20.

Example 3: Generate Multiple Variables with Several Random Numbers

The following code shows how to generate multiple variables in SAS that contain random values:

/*create dataset with variable that contain random value*/
data my_data;
   call streaminit(10);
   do i = 1 to 10;
   x = rand("integer", 1, 20);
   y = rand("integer", 50, 100);
   output;
   end;
run;

/*view dataset*/
proc printdata=my_data;

The x variable contains 10 random integers between 1 and 20 while the y variable contains 10 random integers between 50 and 100.

Additional Resources

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

Cite this article

stats writer (2024). How can I generate random numbers in SAS?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-generate-random-numbers-in-sas/

stats writer. "How can I generate random numbers in SAS?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-generate-random-numbers-in-sas/.

stats writer. "How can I generate random numbers in SAS?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-generate-random-numbers-in-sas/.

stats writer (2024) 'How can I generate random numbers in SAS?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-generate-random-numbers-in-sas/.

[1] stats writer, "How can I generate random numbers in SAS?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How can I generate random numbers in SAS?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top