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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1224 views
  • 7 likes
  • 5 in conversation