Simulation,Need Help Please

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Simulation,Need Help Please

I want to simulate contaminates normal data for given covariance structure. I use the pro gramm from the book "simulating data with SAS" . It work. But I want run this 2000 times and get the output 2000 time. I'll try following way and several ways too. But not working and in log page no any error.

Please can you anybody tell me what is the mistake of this. Thank you.


it =2000

do sim=1 to it;

%let N = 100;

  prociml;

mu =  {0 0 0};                           

Cov = {10  3  -2,  3  6  1, -2  1  2};

k2 = 100;                           
p = 0.1;                                 
call randseed(1);

call randgen(N1, "Binomial,1-p, &N);

X = j(&N,
ncol(mu));

X[1:N1,]= RandNormal(N1, mu, Cov);             

X[N1+1:&N,]= RandNormal(&N-N1, mu, k2*Cov);   

k=rowvec(X);

E=k`;

PRINT E;

end;

End;


Accepted Solutions
Solution
‎06-18-2013 07:32 AM
Super Contributor
Posts: 282

Re: Simulation,Need Help Please

Hi,

Have you tried using a macro function? E.g.:

%macro multiple_iml(multiple);

  %do i=1 %to &multiple;

    < the code you want to run here >

  %end;

%mend multiple_iml;

%multiple_iml(2000);

You can call it to run a different number of times by supplying the required number.

Regards,

Amir.

View solution in original post


All Replies
Solution
‎06-18-2013 07:32 AM
Super Contributor
Posts: 282

Re: Simulation,Need Help Please

Hi,

Have you tried using a macro function? E.g.:

%macro multiple_iml(multiple);

  %do i=1 %to &multiple;

    < the code you want to run here >

  %end;

%mend multiple_iml;

%multiple_iml(2000);

You can call it to run a different number of times by supplying the required number.

Regards,

Amir.

Occasional Contributor
Posts: 8

Re: Simulation,Need Help Please

Hi Amir;

It works..Thank you  very much! I really appreciate it....

SAS Super FREQ
Posts: 3,482

Re: Simulation,Need Help Please

Move the DO loop inside the PROC IML statement. See the examples on p. 69-70 in the book or in this article How to generate multiple samples from the multivariate normal distribution in SAS - The DO Loop

Do not use a macro loop, as explained on p. 100-101

Occasional Contributor
Posts: 8

Re: Simulation,Need Help Please

Dear Dr.Rick...Thank you very much for your help. I was working on this about two week. Even I use your book I didn't understand this until you mentioned. I want to tell this. Your book is great. I love your book. I completed my paper easily because of your book. I have both book you wrote.

Now, This programm is working. But sometimes it doesn't and gives the error "The requested number of observations should be at least 1: 0" What is the reason for this? I thought programme on p.138 is Macro. Isn't it?

Thank you so much For your help!

SAS Super FREQ
Posts: 3,482

Re: Simulation,Need Help Please

You are welcome. I am glad that you find it useful.

The error message is from the RANDNORMAL function.  It says that you are requesting 0 observations.
For values of p close to 0 or 1, it might turn out that N1=0 or &N-N1=0.

To protect against this possibility, use something like this:

if N1>0 then X[1:N1,]= RandNormal(N1, mu, Cov);             

if N1<&N then X[N1+1:&N,]= RandNormal(&N-N1, mu, k2*Cov);

Occasional Contributor
Posts: 8

Re: Simulation,Need Help Please

Thank you sir!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 291 views
  • 4 likes
  • 3 in conversation