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
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;
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.
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;
As PGStats says, CALL STREAM. For a discussion and further examples, see "Independent streams of random numbers in SAS"
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.