Contributor
Posts: 20

# Generating normal variable 50 times

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;
Occasional Contributor
Posts: 5

## Re: Generating normal variable 50 times

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)
SAS Super FREQ
Posts: 4,171

## Re: Generating normal variable 50 times

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...
Discussion stats
• 2 replies
• 282 views
• 0 likes
• 3 in conversation