BookmarkSubscribeRSS Feed
Peter_Y
Calcite | Level 5

Hello:

   I hope to simulate multiple datasets based on a single seed for reproducibility. It seems that call streaminit only impacts randomization within the same data step. For example, if I run the code below

data test1;
  call streaminit(1234);
  do i = 1 to 4;
    a= rand('Uniform');
    output;
  end;
run;
proc print data=test1;
run;

data test2;
  do i = 1 to 4;
    b= rand('Uniform');
    output;
  end;
run;
proc print data=test2;
run;

test1 will remain the same but test2 will change in multiple runs. Is there a way to set seed only once (at the beginning of the program) to determine all random numbers in the entire program, regardless of whether they are in the same data step?

 

Thanks,

Peter

4 REPLIES 4
Kurt_Bremser
Super User

You need a separate CALL STREAMINIT in every step, but you can simplify it with a macro variable:

%let init = call streaminit(1234);

data test1;
&init;
....
run;

data test2;
&init;
...
run;
PGStats
Opal | Level 21

Look at the STREAM routine :

 

%let seed=1234;
data test1;
  call streaminit(&seed.);
  call stream(1);
  do i = 1 to 4;
    a= rand('Uniform');
    output;
  end;
run;
proc print data=test1;
run;

data test2;
  call streaminit(&seed.);
  call stream(2);
  do i = 1 to 4;
    b= rand('Uniform');
    output;
  end;
run;
proc print data=test2;
run;

Both streams are controlled by the same seed but consist of independent sequences.

PG
mkeintz
PROC Star

Your actual problem may preclude this solution, but the code below solves the task as you describe it: 

 

data test1  test2 (rename=(a=b));
  call streaminit(1234);
  do i = 1 to 4;
    a= rand('Uniform');
    output;
  end;
run;

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Rick_SAS
SAS Super FREQ

As PGStats says, CALL STREAM. For a discussion and further examples, see "Independent streams of random numbers in SAS"

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 657 views
  • 7 likes
  • 5 in conversation