SAS Optimization, and SAS Simulation Studio

turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-24-2009 10:03 AM

Can anybody provide a code to create two vectors with random numbers, but given correlation. Thanks in advance

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-25-2009 12:17 PM

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.

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-27-2009 08:54 AM

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.

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-28-2009 09:48 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-28-2009 02:09 PM

SAS Technical Support can give you the code for the RANDNORM module.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-28-2009 12:04 PM

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;

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-28-2009 02:19 PM

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.

The random samples you are generating are never perfectly

uncorrelated, but corr(A,C) should asymptotically approach your target as N --> infinity.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-28-2009 02:39 PM

To Rick only. Thanks, that is what I wanted to hear