/* ******************************************************************** */ /* */ /* This macro creates a random sample from a normal distribution of */ /* size = size1 with mean=mean and standard deviation = std. This */ /* generated data is placed in a file name of your choice = filename. */ /* The variable name for the data is your choice = colmnname. This */ /* This macro either prints this data or not, depending upon the print */ /* option of yes or no. Next, this macro takes a sample from the */ /* already generated sample and places it in a data file name of your */ /* choice = sampname. This sample has size = size2. */ /* */ /* The purpose of this macro is to be able to draw a random sample of */ /* size = n from a simulated normal distribution. e.g. the first */ /* distribution might be of size = 1000; so you may want the print */ /* option to be off. */ /* */ /* Example: Suppose I want to generate 3 random samples of size 5 from */ /* a simulated N(100,5^2) distribution. Code is as follows: */ /* */ /* */ /* %random(nordist1,1000,N100_5_1,100,5,samp1,5,no); */ /* */ /* %random(nordist2,1000,N100_5_2,100,5,samp2,5,no); */ /* */ /* %random(nordist3,1000,N100_5_3,100,10,samp3,5,no); */ /* */ /* data merged; */ /* merge samp1 samp2 samp3; */ /* proc print data=merged; */ /* run; */ /* */ /* */ /* */ /* */ /* ******************************************************************** */ %macro random(filename,size1,colmname,mean,std,sampname,size2,print); data &filename; retain _seed_ 0; do _i_ = 1 to &size1; &colmname = &mean + &std * rannor(_seed_); output; end; drop _seed_ _i_; run; %if &print=yes %then %do; proc print data=&filename; %end; /* Below is rearranging the data set 'filename' by using a uniform random */ /* number generator */ /* No real reason to do this */ proc sql; create view norsam as select *, RANUNI(0) as _ran_ from &filename order by calculated _ran_; quit; %if &print=YES %then %do; proc print data=norsam; %end; /* Below creates new data set of a different sample size */ data &sampname; set norsam(obs = &size2); drop _ran_; proc print data=&sampname; %mend random; /* **************** END OF MACRO ****************** */ |