- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
--------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
As PGStats says, CALL STREAM. For a discussion and further examples, see "Independent streams of random numbers in SAS"