user2391236
user2391236

Reputation:

SAS generate normal Y~N(...)

For my SAS project I have to generate pairs of (X,Y) with a distribution Y ~ N(3 + X + .5X^2, sd = 2). I have looked at all of the SAS documentation for normal() and I see absolutely no way to do this. I have tried many different methods and am very frustrated.

Upvotes: 1

Views: 191

Answers (2)

user667489
user667489

Reputation: 9569

I believe this is an example of what the asker wants to do:

data sample;
    do i = 1 to 1000;
        x = ranuni(1);
        y = rand('normal', 3 + x + 0.5*x**2, 2);
        output;
    end;
run;

proc summary data = sample;
    var x y;
    output out = xy_summary;
run;

Joe is already more or less there - I think the only key point that needed addressing was making the mean of each y depend on the corresponding x, rather than using a single fixed mean for all the pairs. So rather than 1000 samples from the same Normal distribution, the above generates 1 sample from each of 1000 different Normal distributions.

I've used a uniform [0,1] distribution for x, but you could use any distribution you like.

Upvotes: 1

Joe
Joe

Reputation: 63434

You generate random numbers in SAS using the rand function. It has all sorts of distributions available; read the documentation to fully understand.

I'm not sure if you can directly use your PDF, but if you're able to use it with a regular normal distribution, you can do that. On top of that, most of the Univariate DFs SAS supports start out with the Uniform distribution and then apply their formula (Discrete or continuous) to that, so that might be the right way to go. That's heading into stat-land which is somewhere I'm averse to going. There isn't a direct way to simply pass a function for X as far as I know, however.

To generate [numsamp] normals with mean M and standard deviation SD:

%let m=0;
%let sd=2;
%let numsamp=100;

data want;
  call streaminit(7);
  do id = 1 to &numsamp;
    y = rand('Normal',&m.,&sd.);
    output;
  end;
run;

So if I understand what you want right, this might work:

%let m=0;
%let sd=2;
%let numsamp=1000;
data want;
  call streaminit(7);
  do id = 1 to &numsamp;
    x = rand('Normal',&m.,&sd.);
    y = 0.5*x**2 + x + 3;
    output;
  end;
run;

proc means data=want;
var x y;
run;

X has mean 0.5 with SD 1.96 (roughly what you ask for). Y has mean 5 with SD 3.5. If you're asking for Y to have a SD of 2, i'm not sure how to do that.

Upvotes: 1

Related Questions