Contents: |
Purpose / Requirements / Usage / Limitations |

*PURPOSE:*- The %MVN macro generates multivariate normal data using the Cholesky root of the variance-covariance matrix. Bivariate normal data can be generated using the DATA step.
*REQUIREMENTS:*- Version 6 or later of SAS/IML software. The DATA step code for generating bivariate normal data requires only Version 6 Base SAS software.
*USAGE:*-
Follow the instructions in the Downloads tab of this
sample to save the %MVN macro definition. Replace the text within quotes in the following statement with the location of the %MVN macro definition file on your system. In your SAS program or in the SAS editor window, specify this statement to define the %MVN macro and make it available for use:
%inc "<location of your file containing the MVN macro>";

Following this statement, you may call the %MVN macro. See the Results tab for an example.

The following parameters are required except for SEED=:

**VARCOV=**- SAS data set that contains the variance-covariance (and only the variance covariance) matrix. The macro expects m variables and m observations in the data set, where m is the number of variables to generate.
**MEANS=**- SAS data set that contains the mean vector. The macro expects a single variable with m observations containing the m means for the variables generated.
**N=**- Number of observations to generate.
**SEED=**- Starting seed value for the random number generator. Default value is 0, which will use the system clock to generate a seed.
**SAMPLE=**- SAS data set name for the resulting multivariate normal data. The variable names will be COL1-COLm.

*LIMITATIONS:*- No error checking is done. The macro assumes that dataset names entered are valid, and exist in the case of the VARCOV= and MEANS= options.

These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.

Three methods are presented in increasing order of efficiency. In each case, the desired means (MU1 and MU2), variances (VAR1 and VAR2), and correlation coefficient (RHO) are specified. PROC CORR verifies that the generated data have the desired distribution.

data a;
keep x y;
mu1=10; mu2=20; var1=4; var2=9; rho=.5;
do i = 1 to 10000;
x = mu1+sqrt(var1)*rannor(123);
y = (mu2+rho*(sqrt(var2)/sqrt(var1))*(x-mu1)) +
sqrt(var2*(1-rho**2))*rannor(123);
output;
end;
run;
proc corr noprob; run;
data b;
keep x y;
mu1=10; mu2=20; var1=4; var2=9; rho=.5;
do i = 1 to 10000;
/* generate standard normal variates with corr of rho */
x = rannor(123);
y = rho*x+sqrt(1-rho**2)*rannor(123);
/* transform to designated mean and variance */
x = mu1 + sqrt(var1)*x;
y = mu2 + sqrt(var2)*y;
output;
end;
run;
proc corr noprob; run;
data c;
keep x y;
mu1=10; mu2=20; var1=4; var2=9; rho=.5;
std1=sqrt(var1); std2=sqrt(var2);
c=sqrt(1-rho**2);
do i = 1 to 10000;
x = rannor(123);
y = rho*x+c*rannor(123);
x = mu1 + sqrt(var1)*x;
y = mu2 + sqrt(var2)*y;
output;
end;
run;
proc corr noprob; run;

These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.

*EXAMPLE:*-
This example generates 20000 observations from a three-variable,
multivariate normal distribution with specified mean vector and
covariance matrix.
/* Store the variance-covariance matrix in a data set */ data varcov; input m1-m3; cards; 4 1.8 4 1.8 9 3.6 4 3.6 16 ; /* Store the mean vector in a data set */ data means; input m1; cards; 10 20 30 ; /* Define the MVN macro */ %inc "<location of your file containing the MVN macro>"; %mvn(varcov=varcov, means=means, n=20000, sample=test) proc corr data=test noprob cov; run;

*RESULTS:*- The %MVN macro produces only an output data set. The following results are produced by the PROC CORR step showing that the generated data have the specified means and covariance matrix.
The CORR Procedure 3 Variables: COL1 COL2 COL3 Covariance Matrix, DF = 19999 COL1 COL2 COL3 COL1 3.95131676 1.77413626 4.00060059 COL2 1.77413626 9.02756640 3.58973133 COL3 4.00060059 3.58973133 16.18132889 Simple Statistics Variable N Mean Std Dev Sum Minimum Maximum COL1 20000 10.00995 1.98779 200199 2.60355 18.61083 COL2 20000 19.99845 3.00459 399969 8.57479 33.31395 COL3 20000 29.99947 4.02260 599989 15.04103 46.88934

Right-click on the link below and select **Save** to save
the %MVN macro definition
to a file. It is recommended that you name the file
`mvn.sas`

.

The %MVN macro generates multivariate normal data using the
Cholesky root of the variance-covariance matrix. Bivariate normal
data can be generated using the DATA step.

#### Operating System and Release Information

Type: | Sample |

Topic: | Analytics ==> Simulation Analytics ==> Transformations Analytics ==> Regression Analytics ==> Time Series Analysis Analytics ==> Matrix Programming Analytics ==> Mathematical Optimization Analytics ==> Exploratory Data Analysis Analytics ==> Financial Analysis Analytics ==> Forecasting SAS Reference ==> Procedures ==> CORR SAS Reference ==> Procedures ==> IML Analytics ==> Nonparametric Analysis Analytics ==> Descriptive Statistics |

Date Modified: | 2016-06-01 15:36:02 |

Date Created: | 2005-01-13 15:03:27 |

Product Family | Product | Host | SAS Release | |

Starting | Ending | |||

SAS System | SAS/STAT | All | n/a | n/a |

SAS System | SAS/IML | All | n/a | n/a |