• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / SAS / SAS rand – Generate Random Numbers in a SAS Data Step

SAS rand – Generate Random Numbers in a SAS Data Step

January 18, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  catx SAS – Concatenate String Variables with Delimiter in SAS Data Step
  • 2.  SAS Remove Labels from Dataset with PROC DATASETS
  • 3.  SAS Power Function – Exponentiate Numbers with ** in a Data Step
  • 4.  SAS Dollar Format – Formatting Numbers as Dollars in SAS Dataset
  • 5.  Get Substring from Right in SAS
  • 6.  SAS intnx – Add or Subtract Time from Date Variables in SAS Data Step
  • 7.  Identifying Duplicates in SAS with PROC SORT dupout Option
  • 8.  SAS mean() Function – Find Average Across Columns in Data Step
  • 9.  SAS floor – Round Down to Floor of Number in a SAS Data Step
  • 10.  SAS %eval() Function – Evaluate Expressions in SAS Macro

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy