BookmarkSubscribeRSS Feed
aya123
Calcite | Level 5
The following code is used to generate 50 vectors of size n and each vector is variable (E) having normal distribution with mean 0 and variance equals 1, so how can I generate this variable 50 times but when the variable (E) follows normal distribution with mean 0 and variance equals 4 ?

Do j=1 to 50;
E= normal(repeat(-1,n));
end;
2 REPLIES 2
nicolas_
Calcite | Level 5
You just multiply E by the square root of the variance, i.e. E=2#normal(repeat(-1,n));

A linear transformation aX+b of a normal variable X is again normally distributed with mean equal to a*mean(X) + b and variance equal to a^2*var(X)
Rick_SAS
SAS Super FREQ
You can use the RANDGEN call, which includes optional arguments for the mean and Std Dev. You first need to allocate a vector to store the data:
proc iml;
n=10;
E = j(n,1);
do i = 1 to 50;
call randgen(E, "Normal", 0, 4);
end;

You can also get all 50 variables at once. Each column of the following matrix contains a sample from N(0, 4).

E = j(n,50);
call randgen(E, "Normal", 0, 4);

For comments on generating random vectors efficiently, see
http://blogs.sas.com/iml/index.php?/archives/9-Efficient-Sampling.html

For a description of how to generate 50 variables that each have a different distribution, see
http://blogs.sas.com/iml/index.php?/archives/117-How-to-Sample-from-Independent-Normal-Distributions...

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 2 replies
  • 1439 views
  • 0 likes
  • 3 in conversation