If you want to generate random numbers in a SAS data step, the easiest way is to use the SAS **rand()** function.

```
data k;
do i = 1 to 10;
rand_num = rand("Uniform");
output;
end;
run;
```

When working with data, sometimes it can be very useful to generate random numbers to be able to perform simulations or get a random sample of a dataset.

In SAS, we can generate random numbers easily. The SAS **rand()** function can return random numbers from different statistical distributions depending on how we want the resulting properties of our randomly generated dataset.

## Generating Random Numbers in a Range Using SAS

Using SAS, we can generate random numbers uniformly in a range easily. By default, if we pass “Uniform” to the SAS **rand()** function, you will receive random numbers between 0 and 1.

To generate random numbers between 0 and 1, we can do so easily in the following SAS code.

```
data k;
do i = 1 to 10;
rand_num = rand("Uniform");
output;
end;
run;
```

To generate random numbers between numbers a and b, for example, 0 and 10, we need to add by a and then multiple the randomly generated number by the difference between a and b.

```
data k;
a = 0;
b = 10;
do i = 1 to 10;
rand_num = a + (b - a) * rand("Uniform");
output;
end;
run;
```

You will see that the numbers generated from this data step are all between 0 and 10.

```
i rand_num
1 1 9.4039006904
2 2 0.2762846812
3 3 9.4098080415
4 4 8.4096989129
5 5 7.4553070194
6 6 0.1901044999
7 7 8.8871195493
8 8 8.6166257504
9 9 8.9881443954
10 10 0.5240682443
```

## Generating Random Integers in a Range Using SAS

If you want to generate random integers in a range, there are 2 ways you can do it. Depending on your version of SAS, you can pass “integer” to the SAS **rand()** function, or you will need to use the SAS **ceil()** function.

If you have a version of SAS later than SAS 9.4M5, you can pass “integer”, as well as the bounds of your range to generate random integers in a range.

```
data k;
a = 0;
b = 10;
do i = 1 to 10;
rand_int = rand("integer",0,10);
output;
end;
run;
```

If you have a version of SAS earlier than SAS 9.4M5, you have to use the SAS **ceil()** function. We can modify our code from above to generate an integer in a range using the SAS **ceil()** function easily.

```
data k;
a = 0;
b = 10;
do i = 1 to 10;
rand_num = a + ceil((b - a) * rand("Uniform"));
output;
end;
run;
```

As you can see below, we generate 10 integers between 0 and 10

```
i rand_num
1 1 7
2 2 9
3 3 9
4 4 4
5 5 4
6 6 5
7 7 6
8 8 6
9 9 7
10 10 6
```

## Using Different Statistical Distributions to Generate Random Numbers in SAS

We can use the SAS **rand()** function to generate random numbers from different statistical distributions.

For example, if we want to generate normally distributed random numbers, we just pass “Normal” to **rand()**

`rand_norm = rand("Normal");`

You can change the mean and standard deviation by passing arguments specifying these properties:

```
// Normal distribution with mean 0 and standard deviation 1
rand_norm = rand("Normal", 0, 1);
```

You can see all of the different statistical distributions you can use “>here.

Hopefully this article has been useful for you to understand how you can generate random numbers using the SAS **rand()** function in your SAS code.