To generate a random number in SAS, use the RAND function in a DATA step—for example, x = RAND('uniform') returns a number between 0 and 1. For random integers, combine it with FLOOR or INT, such as y = FLOOR(RAND('uniform') * 100) + 1 to get a whole number from 1 to 100.
Why SAS Needs a Random Number Function
SAS is a statistical and data management platform used heavily in clinical trials, finance, and market research. Random numbers are essential for simulation, bootstrapping, Monte Carlo methods, and splitting data into training and validation sets. Unlike Excel’s RAND() which recalculates each time, SAS generates a stream of pseudo-random numbers that you can control and reproduce—critical for regulatory audits and scientific reproducibility.
The Core Function: RAND()
The RAND function is the modern, recommended way to generate random values in SAS. It accepts a distribution name as the first argument, followed by optional parameters. Here are the most common distributions:
RAND('uniform')– a value between 0 and 1 (exclusive of 1).RAND('normal')– a standard normal value (mean 0, std 1).RAND('normal', 50, 10)– normal with mean 50 and standard deviation 10.RAND('integer', 1, 100)– a random integer from 1 to 100.RAND('bernoulli', 0.3)– returns 1 with probability 0.3, else 0.
For example, to generate a dataset with 1,000 rows of uniform and normal values:
data random_data;
do i = 1 to 1000;
uniform_val = RAND('uniform');
normal_val = RAND('normal', 0, 1);
output;
end;
drop i;
run;
This creates a dataset named random_data with two numeric variables.
How to Get Random Integers
While RAND('integer', a, b) directly gives integers between a and b inclusive, many older SAS programs use the uniform function with arithmetic. Both work, but RAND('integer') is clearer. For example, to simulate a dice roll (1–6):
dice = RAND('integer', 1, 6);
If you need a random integer from 0 to 99, use RAND('integer', 0, 99). Note that RAND('integer') is available in SAS 9.4 and later; for older versions, use FLOOR(RAND('uniform') * 100) for 0–99, or INT(RAND('uniform') * 100) + 1 for 1–100.
Setting a Seed for Reproducibility
By default, SAS uses the system clock to seed the random number generator, so each run gives different numbers. To reproduce the same sequence, set a seed with the CALL STREAMINIT routine. Place it before any RAND call in the same DATA step:
data reproducible;
call streaminit(12345);
x = RAND('uniform');
run;
Using the same seed (12345) always yields the same x value. This is vital for clinical trial simulations where results must be auditable. Note that the seed must be a positive integer, and it only affects the current DATA step—each DATA step resets the stream unless you call STREAMINIT again.
Common Mistakes and Pitfalls
One frequent error is using the old UNIFORM(seed) function, which is deprecated and less flexible. Another is forgetting that RAND('uniform') never returns exactly 0 or 1, which matters for some algorithms. Also, if you call RAND inside a macro loop, each iteration may generate the same value unless you reinitialize the stream. To avoid this, use a single DATA step with a DO loop instead of a macro. Finally, be careful with RAND('normal')—it returns values with no bounds, so if you need positive numbers, use a lognormal or uniform distribution.
Comparing RAND() with Other SAS Random Functions
SAS has several legacy functions: UNIFORM(seed), NORMAL(seed), and RANUNI(seed). These require a seed argument each time and are not as flexible. The RAND function is preferred because it covers dozens of distributions (e.g., binomial, Poisson, exponential) and works with STREAMINIT for better control. For example, to generate a Poisson random value with mean 5: RAND('poisson', 5). The legacy functions cannot do this directly.
Using Random Numbers for Sampling and Splitting
A common practical use is to split a dataset into training and test sets. For example, to randomly assign 70% of observations to a training group:
data split;
set original;
call streaminit(42);
if RAND('uniform') < 0.7 then group = 'train';
else group = 'test';
run;
This assigns each row independently. For a stratified split (e.g., by a categorical variable), use PROC SURVEYSELECT with the SEED option, which is more robust for complex designs. But for simple random sampling, the DATA step approach works fine.
Frequently Asked Questions
How do I generate a random number between 1 and 10 in SAS? Use RAND('integer', 1, 10). If your SAS version is older than 9.4, use FLOOR(RAND('uniform') * 10) + 1.
Why do I get the same random numbers every time I run my SAS program? You likely have a CALL STREAMINIT with a fixed seed. That is intentional for reproducibility. To get different numbers each run, remove the STREAMINIT call or use a seed from the system clock (e.g., CALL STREAMINIT(0) is not allowed; just omit it).
Can I generate a random date in SAS? Yes. Generate a random integer between two date numbers, then convert to a date format. For example: date = RAND('integer', '01JAN2020'd, '31DEC2020'd); format date date9.;
Is there a way to generate random numbers without a DATA step? Yes, you can use PROC IML with the RANDGEN subroutine, or use PROC SURVEYSELECT to create random samples. But the DATA step is simplest for most tasks.
What is the difference between RAND and RANUNI? RANUNI(seed) is a legacy function that generates a uniform number and requires a seed argument each time. RAND('uniform') is the modern function, works with STREAMINIT, and supports many distributions. Always prefer RAND.
Related
- Balance Sheet T-Account: How It Works
- How to Calculate Tenure in Excel: Step-by-Step
- Google Sheets SUMIF Syntax & Examples
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- How to Insert a Bold Line in WordTo insert a bold horizontal line in Microsoft Word, place the cursor in a blank paragraph, open Home…
- Excel Print Area Lines: How to View, Set, and Hide ThemYes. In Excel for Microsoft 365 on Windows, the clearest way to see your print area is to…
- Google Sheets Merge Cells: Do They Keep All Values?No—Google Sheets keeps only the value in the upper-left cell when you merge a range; content in the…
- Excel Freeze Panes Shortcut: Windows Keys and TipsIn Excel for Microsoft 365 on Windows, press Alt, then W, F, and F to open Freeze Panes.…
- How to Add a Drop-Down List in Excel for Easy EntryTo add a drop-down list in Excel for Microsoft 365 on Windows, select the cell or cells, choose…
- Excel SUM and Hidden Cells: What Gets Included in Totals?Yes. In Excel for Microsoft 365 on Windows, SUM includes numbers in hidden rows and columns, including rows…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.