## SAS different random number from same seed in different data steps

Solved
Occasional Contributor
Posts: 11

# SAS different random number from same seed in different data steps

hiya all

In SAS you can generate random numbers from the same distribution using a selected seed at each data step: same distribution + same seed in 2 diff data steps = same serie of random number in 2 data steps.

Now I would like to sample from the same distribution (Normal) from the same seed in different data step but I don't want the same random pick.

is there a trick to have 2 different series of random number from the distribution and the same seed in 2 different data steps?

I though of sampling once from the normal distribution and a chosen seed and create a large datafile of random numbers; I could then at each different data steps pick in that list.

Is there something cleverer i wonder?

Many thanks

Accepted Solutions
Solution
‎04-21-2017 04:10 AM
SAS Super FREQ
Posts: 3,839

## Re: SAS different random number from same seed in different data steps

You can simulate many (thousands) of samples using one seed. There are dozens of articles on my blog and a more systematic presentation in the book Simulating Data with SAS, but here's one article that contains several important points: "Simulate many samples from a linear regression model (including ANOVA)"

All Replies
Super User
Posts: 11,801

## Re: SAS different random number from same seed in different data steps

Please explain why using the same seed is important in this case. The "seed" basically sets a position in the stream of random numbers the function generates.

I think this does something along the lines of what your are requesting but without knowing how you intend to use this it is hard know if this will suit your need.

```data work.one;
seed=15;
do i= 1 to 10;
call rannor(seed,x);
if mod(i,2) = 0 then output;
end;
run;

data work.two;
seed=15;
do i= 1 to 10;
call rannor(seed,y);
if mod(i,2) = 1 then output;
end;
run;```

To verify that you are getting numbers from the same series then remove the "if mod() = then" in one or both data sets.

Occasional Contributor
Posts: 11

## Re: SAS different random number from same seed in different data steps

hi Ballardw

I suppose I could elaorated a bit more.

This is a genetic algorithm in which I will repeat a large number of time the simulation (~1,000) in order to estimates variance and covariances between traits.

The fact that the seed is the same across all the repetition allows me to avoid creating spurious correlations due to the change of seeds. By keeping the same seed I can sample in my Normal without having to worry above that sort of bias.

Your solution looks like a good alternative to my giant file of random numbers :-) I'll give it a go.

Another way i thought of testing would be to have different seeds - bare with me - but taken randomly

cheers!

SAS Super FREQ
Posts: 3,839

## Re: SAS different random number from same seed in different data steps

Issues like this sometimes come up when SAS users are trying to run a simulation study and decide to wrap the study in a macro that takes a single seed as an argument. Sometimes programmers are trying to use an inefficient macro loop to generate the data, instead of an efficient simulation that only requires a single seed.

An efficient answer to your question would take into account the task you are trying to accomplish. Could you say more about what you are trying to do?

Without further details, the best I can offer is to mention that the OUTPUT statement takes an optional argument that specifies the output data set. Thus the following DATA set writes the first 1000 observations to the data set 'A', the next 5000 observations to 'B', and the last 100 observations to 'C':

``````data A B C;
call streaminit(12345);
do i = 1 to 1000;
x = rand("Normal");
output A;
end;
do i = 1 to 5000;
x = rand("Normal");
output B;
end;
do i = 1 to 100;
x = rand("Normal");
output C;
end;
run;
``````

However, there is no intrinsic advantage over having multiple data steps, each with a different random seed.

Occasional Contributor
Posts: 11

## Re: SAS different random number from same seed in different data steps

Hi Rick_SAS

thanks for the comment.

I gave a bit more details to Ballardw above on the why.

Thanks for the article link - I will definitely study it.

Your solution is also worth a good try as it would avoid the creation of that big file of random numbers I am using now and that make the code sluggish.

One thing I didn't mention is the fact that I am reproducing in SAS an algorithm done in fortran and where the random number picking was done by the NAG sub routine. I don't much about Fortran hence my SAS effort.

Many thanks for the comment Rick_SAS

Cheers

Solution
‎04-21-2017 04:10 AM
SAS Super FREQ
Posts: 3,839

## Re: SAS different random number from same seed in different data steps

You can simulate many (thousands) of samples using one seed. There are dozens of articles on my blog and a more systematic presentation in the book Simulating Data with SAS, but here's one article that contains several important points: "Simulate many samples from a linear regression model (including ANOVA)"

☑ This topic is solved.