Loop/Macro to Iterate Seed of Random Value Generator

Reply
Occasional Contributor PBG
Occasional Contributor
Posts: 13

Loop/Macro to Iterate Seed of Random Value Generator

[ Edited ]

I have this code which, given an input covariance matrix, randomly draws 1000 values for each of four normally-distributed variables. It's easy enough to change the seed manually to generate another set of 1000 random variable values, but I'm looking to create 1000 or more of these such sets, and to average them together. Can this be done with a sort of loop or macro? Any ideas as to how to go about automating this? Please share. Thanks for your help!

 

Data RandomValueGeneratorStatistics (type=COV);
input _TYPE_ $ _NAME_ $9. variable1 variable2 variable3 variable4;
datalines;
COV variable1 0.999368288 0.009398213 0.075083647 0.453177098
COV variable2 0.009398213 0.000155024 0.000878636 0.006372190
COV variable3 0.075083647 0.000878636 0.008894056 0.043200581
COV variable4 0.453177098 0.006372190 0.043200581 0.999368288
MEAN          3.217123172 42.69388108 3.893316488 8605.147733
;
run; 

Proc Simnorm data=RandomValueGeneratorStatistics outsim=ssim
numreal = 1000
seed = 54321; 
var variable1 variable2 variable3 variable4;
run;

 

SAS Employee
Posts: 119

Re: Loop/Macro to Iterate Seed of Random Value Generator

Can't you just generate 1000 x 1000 observations in one data set and then process them?  Why change the seed?

Occasional Contributor PBG
Occasional Contributor
Posts: 13

Re: Loop/Macro to Iterate Seed of Random Value Generator

Thanks for your response @WarrenKuhfeld. Yes, that could be done, but I would like to vary the seed to average-out the biases (some of the psuedorandomness) of any single seed. Or is this logic flawed? What role does the seed play in the generation of random values, precisely?

 

In any case, as I'm entirely new to SAS, this could be a great learning experience for me as it pertains to loops/macros.

 

Thanks so much for your help.

Super User
Posts: 17,963

Re: Loop/Macro to Iterate Seed of Random Value Generator

You need to understand random number streams in general, not specifically for SAS.

 

A seed sets the initial value, so you always get the same 'random' numbers with the same seed. 

Using a seed allows you to replicate the stream to verify your results.

So changing the seed in a process doesn't quite make sense because you're likely going to either set those manually or not be able to replicate the data.

 

 

SAS Employee
Posts: 119

Re: Loop/Macro to Iterate Seed of Random Value Generator

 


Check out one of Rick Wicklin's several blogs on seeds.  http://blogs.sas.com/content/iml/2017/06/01/choose-seed-random-number.html 

 

I would recommend doing vanilla DATA step programming to get your results rather than using a macro.  There are plenty of opportunities for macro loops when they are really needed.  I don't know off hand how simnorm generates random numbers.  However, if you want to see the strange things that might happen if you pick the wrong seed combinations with the old rannor/ranuni style functions, see the graphs in this page.  I love the "bug plot" about half way down.

Occasional Contributor PBG
Occasional Contributor
Posts: 13

Re: Loop/Macro to Iterate Seed of Random Value Generator

Thank you both for your help.
SAS Employee
Posts: 119

Re: Loop/Macro to Iterate Seed of Random Value Generator

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a001281561.htm

 

Looks like I failed to post the link.  This page does answer some of your questions about seeds and what can go wrong when you change seeds.

Super User
Posts: 17,963

Re: Loop/Macro to Iterate Seed of Random Value Generator

If you're doing simulations in SAS this is a good foundational paper to read:

 

http://www2.sas.com/proceedings/forum2007/183-2007.pdf

Occasional Contributor PBG
Occasional Contributor
Posts: 13

Re: Loop/Macro to Iterate Seed of Random Value Generator

Thanks

Ask a Question
Discussion stats
  • 8 replies
  • 218 views
  • 10 likes
  • 3 in conversation