BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Can anybody provide a code to create two vectors with random numbers, but given correlation. Thanks in advance
7 REPLIES 7
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.


GOOGLE advanced search argument - note the use of the parameter site: in your search argument:

generate random number vector with correlation site:sas.com

Good luck.


Scott Barry
SBBWorks, Inc.
Rick_SAS
SAS Super FREQ
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.
deleted_user
Not applicable
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.
Rick_SAS
SAS Super FREQ
SAS Technical Support can give you the code for the RANDNORM module.
deleted_user
Not applicable
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.
Big thanks in advance.

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;
Rick_SAS
SAS Super FREQ
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.
deleted_user
Not applicable
To Rick only. Thanks, that is what I wanted to hear

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 1801 views
  • 0 likes
  • 3 in conversation