BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Muthu
Fluorite | Level 6

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Amir
PROC Star

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

6 REPLIES 6
Amir
PROC Star

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.

Muthu
Fluorite | Level 6

Hi Amir;

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

Rick_SAS
SAS Super FREQ

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

Muthu
Fluorite | Level 6

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!

Rick_SAS
SAS Super FREQ

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);

Muthu
Fluorite | Level 6

Thank you sir!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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