The
DATA steps in this section illustrate several properties of the random-number
functions. Each of the DATA steps that call a function generates a
single stream of pseudo-random numbers based on a seed value of 7,
because that is the first seed for the first call for every step.
Some of the DATA steps change the seed value in various ways. Some
of the steps have single function calls and others have multiple function
calls. None of these DATA steps change the seed. The only seed that
is relevant to the function calls is the seed that was used with the
first execution of the first random-number function. There is no way
to create separate streams with functions (CALL routines are used
for this purpose), and the only way that you can restart the function
random-number stream is to start a new DATA step.
The following example
executes multiple DATA steps:
/* This DATA step produces a single stream of random numbers */
/* based on a seed value of 7. */
data a;
a = ranuni (7); output;
a = ranuni (7); output;
a = ranuni (7); output;
a = ranuni (7); output;
a = ranuni (7); output;
a = ranuni (7); output;
a = ranuni (7); output;
a = ranuni (7); output;
a = ranuni (7); output;
a = ranuni (7); output;
a = ranuni (7); output;
a = ranuni (7); output;
run;
/* This DATA step uses a DO statement to produce a single */
/* stream of random numbers based on a seed value of 7. */
data b (drop = i);
do i = 7 to 18;
b = ranuni (i);
output;
end;
run;
/* This DATA step uses a DO statement to produce a single */
/* stream of random numbers based on a seed value of 7. */
data c (drop = i);
do i = 1 to 12;
c = ranuni (7);
output;
end;
run;
/* This DATA step calls the RANUNI and the RANNOR functions */
/* and produces a single stream of random numbers based on */
/* a seed value of 7. */
data d;
d = ranuni (7); f = ' '; output;
d = ranuni (8); f = ' '; output;
d = rannor (9); f = 'n'; output;
d = .; f = ' '; output;
d = ranuni (0); f = ' '; output;
d = ranuni (1); f = ' '; output;
d = rannor (2); f = 'n'; output;
d = .; f = ' '; output;
d = ranuni (3); f = ' '; output;
d = ranuni (4); f = ' '; output;
d = rannor (5); f = 'n'; output;
d = .; f = ' '; output;
run;
/* This DATA step calls the RANNOR function and produces a */
/* single stream of random numbers based on a seed value of 7. */
data e (drop = i);
do i = 1 to 6;
e = rannor (7); output;
e = .; output;
end;
run;
/* This DATA step merges the output data sets that were */
/* created from the previous five DATA steps. */
data five;
merge a b c d e;
run;
/* This procedure writes the output from the merged data sets. */
proc print label data=five;
options missing = ' ';
label f = '00'x;
title 'Single Random Number Streams';
run;
The following output
shows the program results.
Results from Generating a Single Random-Number Stream
The pseudo-random number
streams in output data sets A, B, and C are identical. The stream
in output data set D mixes calls to the RANUNI and the RANNOR functions.
In observations 1, 2, 5, 6, 9, and 10, the values that are returned
by RANUNI exactly match the values in the previous streams. Observations
3, 7, and 11, which are flagged by “n”, contain the
values that are returned by the RANNOR function. The mix of the function
calls does not affect the generation of the pseudo-random number stream.
All of the results are based on a single stream of uniformly distributed
values, some of which are transformed and returned from other functions
such as RANNOR. The results of the RANNOR function are produced from
two internal calls to RANUNI. The DATA step that creates output data
set D executes the following steps three times to create 12 observations:
-
-
-
call to RANNOR (which internally
calls RANUNI twice)
-
skipped line to compensate for
the second internal call to RANUNI
In the DATA step that
creates data set E, RANNOR is called six times, each time skipping
a line to compensate for the fact that two internal calls to RANUNI
are made for each call to RANNOR. Notice that the three values that
are returned from RANNOR in the DATA step that creates data set D
match the corresponding values in data set E.