N/A
Posts: 0

How to create two sets of random numbers with given correlation coefficient

Can anybody provide a code to create two vectors with random numbers, but given correlation. Thanks in advance
Super Contributor
Posts: 3,176

Re: How to create two sets of random numbers with given correlation coefficient

Though I do not have an answer to your request, I highly recommend using the SAS support http://support.sas.com/ website and its SEARCH facility using some of your terms and/or phrases in your SUBJECT. Also, I did a Google advanced search (argument listed below) against the SAS.COM website and found some search-matches which you may find useful, both SAS documentation and also supplemental technical/conference papers.

generate random number vector with correlation site:sas.com

Good luck.

Scott Barry
SBBWorks, Inc.
SAS Super FREQ
Posts: 4,109

Re: How to create two sets of random numbers with given correlation coefficient

As stated, your problem doesn't have a unique solution. There are infinitely many pairs of numbers that yield the same correlation coefficient. For example, if x={1, -1}, then any y of the form y={t, -t} has perfect correlation (r=1) with x. Also, the correlation coefficient is invariant under shifts in the mean of the data, so you probably want additional assumptions on the distribution of the data.

A problem that makes sense to ask is "given a correlation (or covariance), can I generate multivariate normal vectors x and y of length N from a bivariate distribution with a given set of means and the given covariance?" The answer is yes, and you can use the RANDNORMAL module in SAS/IML to generate the data. See the RANDNORMAL module documentation for syntax and an example.

Obviously, the particular sample you generate is not guaranteed to have the given mean and covariance, even though it was generated from a distribution with those properties.
N/A
Posts: 0

Re: How to create two sets of random numbers with given correlation coefficient

To Rick only. Please, give me your SAS code that answear to my question for arbitrary mean and sigma. Everything else you talked I know how to do. I don't have randnormal in my sas EG 4. Thanks for your reply.
SAS Super FREQ
Posts: 4,109

Re: How to create two sets of random numbers with given correlation coefficient

SAS Technical Support can give you the code for the RANDNORM module.
N/A
Posts: 0

Re: How to create two sets of random numbers with given correlation coefficient

To Rick only. Could you check this simple code and say if anything is wrong?
Later I will create datasets with given mean and sigma with this correlation.
I cannot get my given correlation factor 0.5. I am getting 0.54.

data test1;
do i=1 to 100;
corr=0.5;
A=rand('NORMAL');
B=rand('NORMAL');
C=corr*A+sqrt(1.0-corr*corr)*B;
output;
end;
run;

proc corr data=test1;
var A; with C;
run;
SAS Super FREQ
Posts: 4,109

Re: How to create two sets of random numbers with given correlation coefficient

Your formula will work if A and B are uncorrelated.
The random samples you are generating are never perfectly
uncorrelated, but corr(A,C) should asymptotically approach your target as N --> infinity.
N/A
Posts: 0

Re: How to create two sets of random numbers with given correlation coefficient

To Rick only. Thanks, that is what I wanted to hear
Discussion stats
• 7 replies
• 600 views
• 0 likes
• 3 in conversation