Solved
Posts: 3,018

# How to create a random number seed at the start of a simulation

I have a large simulation, that involves several data steps and a few loops.

I would like to set a single random number seed at the start of the simulation, and thus have a single reproducible stream of random numbers. How can I do this?

Here is the simulation, in pseudo-code

Loop 1

Data step A creating uniform random variables

Loop 2

Data step B creating more uniform random variables

More PROCs and Data steps

Data step C creating more uniform random variables

PROC SOMETHING

End of Loop 2

End of Loop 1

--
Paige Miller

Accepted Solutions
Solution
‎10-04-2013 02:53 PM
Posts: 5,529

## Re: How to create a random number seed at the start of a simulation

Posted in reply to data_null__

In fact, all procedures (or almost) will update the SYSRANEND macro variable. Although not perfect (check the documentation) this seems to be the best method to carry a seed from one procedure to the next. - PG

PG

All Replies
Posts: 3,852

## Re: How to create a random number seed at the start of a simulation

Posted in reply to PaigeMiller

Perhaps you want to save the last SEED used at each step/interation as the SEED for the next.

If you use the CALL version of the random number function you can track the seed.

Also SURVEYSELECT will create macro variables with seed info.

proc surveyselect data=sashelp.class out=sample rate=.5;

run;
%put NOTE: &=SYSRANDOM &=SYSRANEND;
Solution
‎10-04-2013 02:53 PM
Posts: 5,529

## Re: How to create a random number seed at the start of a simulation

Posted in reply to data_null__

In fact, all procedures (or almost) will update the SYSRANEND macro variable. Although not perfect (check the documentation) this seems to be the best method to carry a seed from one procedure to the next. - PG

PG
Posts: 3,852

PROC Star
Posts: 8,164

## Re: How to create a random number seed at the start of a simulation

Posted in reply to PaigeMiller

Paige,

I had come up with the same idea as DataNull did .. i.e., capturing the latest seed in a datastep using call ranuni.  The following code shows that it produces the same result as I think you are trying to achieve:

data part1;

seed=872643;

do i=1 to 10;

call ranuni(seed,random);

output;

end;

call symput('seed',seed);

run;

data part2;

seed=&seed.;

do i=1 to 10;

call ranuni(seed,random);

output;

end;

call symput('seed',seed);

run;

data part3;

seed=&seed.;

do i=1 to 10;

call ranuni(seed,random);

output;

end;

call symput('seed',seed);

run;

data test;

do i=1 to 30;

random=ranuni(872643);

output;

end;

run;

HTH,

Art

Posts: 3,852

## Re: How to create a random number seed at the start of a simulation

Posted in reply to PaigeMiller

Example of using last seed as next seed with data steps.

%let seed=121853;
data one;
set sashelp.class end=eof;
retain seed &seed;
if _n_ eq 1 then put 'NOTE: ' seed=;
call ranuni(seed,y);
run;
proc print;

run;
data two;
if _n_ eq 1 then do;

set one(keep=seed) point=nobs nobs=nobs;
put 'NOTE: ' seed=;
end;

set sashelp.class;
call ranuni(seed,y2);
run;
proc print;

run;
Posts: 3,018

## Re: How to create a random number seed at the start of a simulation

Posted in reply to data_null__

Thanks to all, looks like I have several solutions.

I think I just need a hotline to , and , and should just skip the forum entirely

--
Paige Miller
Super Contributor
Posts: 1,636

## Re: How to create a random number seed at the start of a simulation

Posted in reply to PaigeMiller

Paige,

You got great solutions in an hour. You are the luckiest man today and should go out to buy lottery tickets.

Posts: 3,018

## Re: How to create a random number seed at the start of a simulation

```Linlin wrote:

Paige,
You got great solutions in an hour. You are the luckiest man today and should go out to buy lottery tickets.
```

Statisticians don't believe in purchasing lottery tickets, the expected gain is negative, worse than just putting money in a savings account that pays 0.5% annually.

--
Paige Miller
PROC Star
Posts: 8,164

## Re: How to create a random number seed at the start of a simulation

Posted in reply to PaigeMiller

, Sure am glad I'm not a statistician.  Conversely, putting money into a savings account that pays 0.5% annually, only has the potential of resulting in a loss when one considers the effects of inflation and the taxes one has to be on the interest. While the likelihood of winning the grand prize of tomorrow's Canadian lottery (\$26 million) has rather poor odds ( 1 in 13,983,816) the potential IS a bit larger than a savings account, the psychological excitement about the possibility of winning exists and, in the unlikely event that one does actually win, the winnings are tax free.

Posts: 3,018

## Re: How to create a random number seed at the start of a simulation

Compared to my friends who regularly play the lottery and football pools and the like, I have not lost a single penny, while they have lost thousands of dollars. I like my situation better than theirs.

My ex-mother-in-law bought 5 dollars in Lottery tickets every week that I knew her. Once I asked her if she ever won anything, and she very enthusiastically replied that Yes, once, she won \$50.

Good luck with your lottery tickets.

--
Paige Miller
Super User
Posts: 13,542

## Re: How to create a random number seed at the start of a simulation

Posted in reply to PaigeMiller

I'm wondering where I can get 0.5% on a savings account. Locally with my balance it's closer to 0.01%.

Of course if I only buy a lottery ticket with the max payout is large enough then the expected winning become positive. But living long enough to play enough times ...:smileylaugh:

Posts: 5,529

## Re: How to create a random number seed at the start of a simulation

Posted in reply to PaigeMiller

One quote I like:

"Limits on knowledge and computation are central to the human condition."

by Herbert A. Simon in Models of Bounded Rationality: Empirically grounded economic reason, MIT Press, 1982

Many profitable industries depend on those limits: insurance, lotteries and casinos, marketing, stock trading, to name a few. And I never forget that some might want to add statistics to the list.

PG

PG
SAS Super FREQ
Posts: 4,241

## Re: How to create a random number seed at the start of a simulation

Posted in reply to PaigeMiller

This is covered on p. 33-34 of my book. Assuming that "Loop 1" is a macro loop, I think you can get rid of it and gain some efficiency in your simulation by using BY processing. See Chapter 6 of my book or see the article Simulation in SAS

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
• 13 replies
• 3089 views
• 7 likes
• 7 in conversation