options ps=65;

title 'SAS IML example3';

 

/*generating random multivariate normal points*/

proc iml;

  do i=1 to 10;

     z=normal(i);

     print z;

     maxz=maxz//z; *concatenate z's;

  end*endo here;

  print maxz;

quit;

 

proc iml;

 start rnorm(mu,sigma,seed);

   z=normal(seed);

   g=root(sigma);

   x=mu+t(g)*z;

   return(x);

 finish;

 /*define a module (function) that generate

 multivariate normal points with mu and sigma*/

 

 a={1 2 3};

 b={ 1   0.7  0.2

    0.7  2   -0.8,

    0.2 -0.8  2.5};

 c={12345 87948 298765};

 do i=1 to 3;

   x=rnorm(t(a),b, t(c));

   print x;

   matx=matx//t(x);

 end*generating 3 3-d multivariate points;

 print matx;

quit;

 

proc iml;

 seed=543219876;

 n=4;

 sigma={4 2 1, 2 3 1, 1 1 5};

 mu={1,2,0};

 p=nrow(sigma);

 m=repeat(t(mu),n,1);  *nx1 mu;

 g=root(sigma);

 z=normal(repeat(seed,n,p)); *nxp n(0,1) points;

 y=z*g+m;

 print y;

quit;

 

data normal; *create a data set "normal" with variables y1 y2 z

 mu_1=0.0;

 mu_2=0.0;

 vy1=2;

 vy2=1;

 rho=0.5;

 

 keep y1 y2 z;

 label z='Density';

 

 con=1/(2*3.141592654*sqrt(vy1*vy2*(1-rho*rho)));

 do y1=-4 to 4 by 0.10;

 do y2=-3 to 3 by 0.10;

   zy1=(y1-mu_1)/sqrt(vy1);

   zy2=(y2-mu_2)/sqrt(vy2);

   hy=zy1**2+zy2**2-2*rho*zy1*zy2;

   z=con*exp(-hy/(2*(1-rho**2)));

   if z>0.001 then output;

 end;

 end;

run;

 

libname mylib 'C:\\Documents and Settings\DB\Desktop\zuo\My Documents\stt843\dataset';

 

proc g3d data=normal gout=mylib.psfile;

  plot y1*y2=z;

  scatter y1*y2=z/color='blue' rotate=30;

run;