BookmarkSubscribeRSS Feed
Nichlas
Calcite | Level 5

Why do I get different results for variable a and variable b even though I use same random seed 10 for the two variables?

 

data rn;
	a=rannor(10);
	b=rannor(10);
run;

 

4 REPLIES 4
PGStats
Opal | Level 21

Please read Rick Wicklin's blog entry on this topic

 

http://blogs.sas.com/content/iml/2013/07/10/stop-using-ranuni.html

 

 

PG
Nichlas
Calcite | Level 5

Thanks, it's because in a data step, the first seed encountered will determine the whole stream, right?

Another question, then can I write

b=rannor

without assigning a seed for the second variable since whatever seed set to b would prove to be useless?

PGStats
Opal | Level 21

Follow Rick's advice and use

 

data rn;
	call streaminit(10);
	a = rand("NORMAL");
	b = a;
run;
PG
Rick_SAS
SAS Super FREQ

In your example, it doesn't matter what seed value you use for b. You will always get

a=0.84963 b=0.70089

 

A random number seed initializes a sequence of pseudorandom values. For the RANUNI function (and other RAN* functions), the first time you use a seed  determines the sequence for the rest of the data step.

 

Here are two related articles:

Random number seeds: Only the first seed matters

Random number streams in SAS: How do they work?

 

My mental model for a stream is this: I have a spiral notebook with thousands of sheets of papers.  On each page, I write down a sequence of random numbers.  The first sheet can have the same numbers as another sheet, but they are in different orders. 

 

The "seed" corresponds to a page. When you want a sequence of random numbers, you tell me what page you want.  I then read off the numbers on that page one at a time.  If you want a different sequence, you need to ask for a different page.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 968 views
  • 0 likes
  • 3 in conversation