You can use SAS to generate random integers between 1–10 or in the range 1–100. This article shows how to generate random integers as easily as Excel does.
I was recently talking with some SAS customers and I was asked "Why can't SAS create an easy way to generate random numbers? Excel has a simple way to generate random numbers between 1 and 100, and I use it all the time."
The speaker was talking about generating random integers from a discrete uniform distribution, where the numbers range between a specified minimum and maximum value. I have previously written about how to generate random numbers in SAS, but the section about random integers is buried in the middle. So let's simplify the process and see whether we can get SAS to generate random integers "as easily as Excel does."
Built-in support for random integers
If you have access to SAS 9.4M5, you can generate random integers directly. The RAND function supports the "Integer" distribution and supports parameters that specify the min and max. To generate a random integer between 1 and 10 you can use the following statements:
data RandInt; do i = 1 to 100; x = rand("Integer", 1, 10); /* requires SAS 9.4M4 or later */ output; end; run;
A macro with a simple syntax
For older versions of SAS, you can define a macro. I looked up the Excel function that generates random integers. The syntax is RANDBETWEEN(min, max). For example, if you want a random integer between 1 and 10, the syntax is RANDBETWEEN(1, 10). For a random integer between 1 and 100, use RANDBETWEEN(1, 100).
The following SAS macro hides the complexity of the RAND function and creates a simple statement that that has the same functionality as the Excel function. It generates random uniform numbers in (0, 1), then transforms them to integers in [min, max]:
/* SAS macro that duplicates the Excel RANDBETWEEN function */ %macro RandBetween(min, max); (&min + floor((1+&max-&min)*rand("uniform"))) %mend;
After you've defined the macro, you can just pretend that it is a function and use it in a DATA step. The following DATA step generates 100 random numbers between 1 and 10:
data RandInt; do i = 1 to 100; x = %RandBetween(1, 10); output; end; run; proc print data=RandInt(obs=6); var x; run;
The adjacent table shows the first six random integers. Every time you run the program you will get a different sequence of random integers. Use the STREAMINIT subroutine if you want a reproducible stream of integers.
A SAS/IML function for random integers
The previous sections are for DATA step programmers. SAS/IML 14.2 also supports the "Integer" distribution. For earlier versions of SAS/IML, you can define a function that takes an additional argument and returns a vector of random integers:
proc iml; /* Generate n random integers drawn uniformly at random from integers in [min, max] where min and max are integers. */ start RandBetween(n, min, max); u = j(n, 1); call randgen(u, "Uniform", min, max+1); return( floor(u) ); finish; /* Test the function. Generate 10,000 values and tabulate the result. */ call randseed(1234); x = RandBetween(10000, 1, 10); call tabulate(val, freq, x); print freq[c=(char(val))];
The output shows that each integer 1 through 10 appeared approximately 1,000 times in the simulated data.
In summary, modern versions of SAS support the "Integer" distribution for generating random integers. For older versions of SAS, you can define a SAS macro that hides the complexity of the RAND function. The result is a syntax that is easy to use and duplicates the functionality of the Excel RANDBETWEEN function.