BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PaigeMiller
Diamond | Level 26

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
1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

View solution in original post

13 REPLIES 13
data_null__
Jade | Level 19

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;
PGStats
Opal | Level 21

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
art297
Opal | Level 21

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

data_null__
Jade | Level 19

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;
PaigeMiller
Diamond | Level 26

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 Smiley Wink

--
Paige Miller
Linlin
Lapis Lazuli | Level 10

Paige,

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

PaigeMiller
Diamond | Level 26

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. Smiley Happy

--
Paige Miller
art297
Opal | Level 21

, 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.

PaigeMiller
Diamond | Level 26

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
ballardw
Super User

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:

PGStats
Opal | Level 21

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
Rick_SAS
SAS Super FREQ

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 13 replies
  • 6767 views
  • 7 likes
  • 7 in conversation