BookmarkSubscribeRSS Feed
Fluorite | Level 6

My problem is that I should be able to simulate IPD for each studies (5) and each simulation (1000)

Let varying the mean effects in each studies and each simulation. I am simulating three outcomes.

I can do something like this to let varying the mean effects in each studies


proc iml;
N = 5;
Meant = {4, 5, 15};
Cov = {1.2 0.29 0.27, 0.29 1.7 0.32, 0.27 0.32 1.5};
call randseed(100);
M = RandNormal(N, Meant, Cov);
D = colvec(repeat(T(1:NumSamples), 1, N));
Z = M || D;
create work.Mt from Z [c={"x1" "x2" "x3"}]; append from Z;
close work.Mt;


But my problem is to let vary these mean for each 1000 sims.

Do you understand the question?
Have you got time to help me a little bit or to suggest me the best solution?


There are two common ways to interpret your question:

1) Each study has a known mean vector that you want to use.  (and possible the covariance matrix also varies between studies.) In that case, make a matrix for which each row contains the mean vector (parameters) for the study, like this:

proc iml;
N = 3;         /* sample size */
Meant = {4 5  15,
         3 4  12,
         3 5  11,
         4 6  10,
         4  7  14};
NumStudies = nrow(Meant);
Cov = {1.2 0.29 0.27, 0.29 1.7 0.32, 0.27 0.32 1.5};
call randseed(100);
Z = j(1, ncol(Meant)+2, .);
create work.Mt from Z [c={"x1" "x2" "x3" "Sample" "Study"}];
do i = 1 to NumStudies; 
   mean = Meant[i,];
   M = RandNormal(N, Mean, Cov);
   Sample = j(N, 1, 1);
   Study = j(N, 1, i);
   Z = M || Sample || Study;
   append from Z;
close work.Mt;

proc print; run;

2) The other interpretation is that you want the mean vectors to be a random draw from a MVNormal distribution. In this case, you genearte the mean vectors first and then proceed as above.


Fluorite | Level 6

Thank you again Rick,


I think that I would like the true study-specific effects to be allowed to be different in each simulation. So i think that the second interpretation is the one that suit for me.


Forthermore In your code I see Cov that I assume to be the within variance-covariance matrix and i would like to consider here also the between study variability.


Do you understand?




Yes, I understand.  I've shown you the basic idea. Now go off and implement your specific design.  If you get stuck, you can post your new program and describe what doesn't work.

Fluorite | Level 6

To continue this interesting conversation. I am trying to run this code


proc iml;                                                                                                                                                                                                   N = 5;   

/* N is the number of 5 studies*/                                                                             


/* I am performing N = 1000 simulations*/ 

Meant = {4, 5, 15};        

/* Vector of Means */                     

Cov = {1.2 0.29 0.27, 0.29 1.7 0.32, 0.27 0.32 1.5};    

/* Between-Studies Variance-Covariance Matrix */                                                                                                                                                                    

call randseed(100);                                                                                                                                                                                        

 M = RandNormal(N*NumSamples, Meant, Cov);         

/* RandNormal function*/                                                                                                                    


 D = colvec(repeat(T(1:NumSamples), 1, N));            

/* I create a matrix D*/                                                                                                                                                                       

Z = M || D;                                            /* I apply Concatenate fucnction*/                                                                                                                                                                                                                 

create work.Mt from Z [c={"x1" "x2" "x3"}]; append from Z;                       

/* I create Matrix Mt with all the means for each studies and each simulation*/                                                                                                                                                                                

close  work.Mt;                                                                                                                                                                                         do i= 1 to NumSamples; /* here start the do loop */                       

MeanT1 = M[i,1:3];
MeanT2 = M[i,1:3];
MeanT3 = M[i,1:3];                                     

MeanT4 = M[i,1:3];                                                                                                                                                                                   

MeanT5 = M[i,1:3];                                                                                                                                                                                   

end;                            /* here finish the do loop*/                                    

NumSamples1=1000;                                                                                                                                                                               MeanP = {9, 8, 21};                                                                                                                                                                        

Cov = {49 7 5.6, 7 25 4, 5.6 4 16};                                               

call randseed(100);    

ST1 = RandNormal(N1t*NumSamples1, MeanT1, Cov);
ST2 = RandNormal(N2t*NumSamples1, MeanT2, Cov);
ST3 = RandNormal(N3t*NumSamples1, MeanT3, Cov);
ST4 = RandNormal(N4t*NumSamples1, MeanT4, Cov);
ST5 = RandNormal(N5t*NumSamples1, MeanT5, Cov);
SP1 = RandNormal(N1p*NumSamples1, MeanP, Cov);
SP2 = RandNormal(N2p*NumSamples1, MeanP, Cov);
SP3 = RandNormal(N3p*NumSamples1, MeanP, Cov);
SP4 = RandNormal(N4p*NumSamples1, MeanP, Cov);
SP5 = RandNormal(N5p*NumSamples1, MeanP, Cov);

My questions are three:


1) Is the do-loop in the right position? if not where I should shift the do-loop?

2) Is the start and end of the do-loop in the right position? If not where I should shift the start and the end?

3) If I create a matrix/table Mt with 4 columns and 5000 columns where the last column contain the index that should score for the do loop how could I recall in the correct way only the vector of three means to generate my IPD?


Do you understand the questions?


It looks like your program is missing the end of several statements (closing brackets and semicolons). Please edit your message, delete the current code, and use the "Insert SAS Code" icon to paste the program into the message. 

Fluorite | Level 6

Dear Rick according to this proc iml with the do loop that you sent me as example and taking in account the second hypothesis that you were suggested I tried to create another proc iml with two nested do loop but apparently it seems to work only the first do loop.


proc iml;
N = 5;         /* study sizes */
MeanP = {9, 8, 21}; /* Non treated mean vector, 3 outcomes */

NumSim=1000; /* Number of simulations */
Sizes = {30, 40, 50, 60, 70} /* Total size for each of the 5 study */                               
Meant = {4, 5, 15}; /* treated mean vector, 3 outcomes */

CovBw = {1.2 0.29 0.27, /*Var-cov between studies */
          0.29 1.7 0.32, 
          0.27 0.32 1.5};                                                                                                                                                                                                            

CovWn = {49 7 5.6, /*Var-cov within studies */
        7 25 4, 
        5.6 4 16};

ZT = j(1, 5, .);
ZP = j(1, 5, .);
create work.DT from ZT [c={"x1" "x2" "x3" "Sample" "Study"}];
create work.DP from ZP [c={"x1" "x2" "x3" "Sample" "Study"}];

do i = 1 to NumSim; /* Start main loop */
	do n= 1 to N; /* Start simulation loop for each sample size*/
SampleSize = Sizes[n];                                                                                                        call randseed(100);                                                                                                                                                                                                                 M[i] = RandNormal(1, Meant, CovBw);

     	ST[i] = RandNormal(SampleSize, M[i], CovWn);                                        
     	SP[i] = RandNormal(SampleSize, MeanP,CovWn);                                                                                                                                

	Sample = j(SampleSize, 1, N);
        Study = j(SampleSize, 1, SampleSize); 

	ZT = ST[i] || Sample || Study;
	append from ZT;

	ZP = SP[i] || Sample || Study;
	append from ZP;


close work.DT;
close work.DP;

But SAS is returning me these errors.


98   end;

NOTE: Module RANDNORMAL loaded from the storage SASHELP.IMLMLIB.

NOTE: Module ROWVEC loaded from the storage SASHELP.IMLMLIB.

99   close work.DT;

NOTE: The data set WORK.DT has 0 observations and 5 variables.

100 close work.DP;

NOTE: The data set WORK.DP has 60000 observations and 5 variables.

101 quit;

NOTE: Exiting IML.

NOTE: PROCEDURE IML used (Total process time):

     real time           19.32 seconds

     cpu time           1.18 seconds


Furthermore it seems to work only for work.DP.


Do you understand what could be the error and how could I solve this?


1) Again, you seem to have problems pasting your program.  Your programs are missing portions of code. Please try again.  If you are having problems. then use the "Attachements" feature to upload the program.

2) I do not see any errors in the log that you posted. I only see six NOTEs. A NOTE is not an error; it just let's you know that certain events (like writing a data set) occurred.



Time is running out to save with the early bird rate. Register by Friday, March 1 for just $695 - $100 off the standard rate.


Check out the agenda and get ready for a jam-packed event featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events. 


Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 7 replies
  • 1 like
  • 2 in conversation