%macro mvn(version, varcov=, /* dataset for variance-covariance matrix */ means=, /* dataset for mean vector */ n=, /* sample size */ seed=0, /* seed for random number generator */ sample=); /* output dataset name */ %if &version ne %then %put MVN macro Version 1.0; /* Get initial seed value. If seed<=0, then generate seed from the system clock. */ data _null_; if &seed le 0 then do; seed = int(time()); /* get clock time in integer seconds */ put seed=; call symput('seed',seed); /* store seed as macro variable */ end; run; /* Generate the multivariate normal data in SAS/IML */ proc iml worksize=100; use &varcov; /* read variance-covariance matrix */ read all into cov; use &means; /* read means */ read all into mu; v=nrow(cov); /* calculate number of variables */ n=&n; seed = &seed; l=t(root(cov)); /* calculate cholesky root of cov matrix */ z=normal(j(v,&n,&seed));/* generate nvars*samplesize normals */ x=l*z; /* premultiply by cholesky root */ x=repeat(mu,1,&n)+x; /* add in the means */ tx=t(x); create &sample from tx; /* write out sample data to sas dataset */ append from tx; quit; %mend mvn;