Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: FW: random number sequence within a loop


From   Raoul C Reulen <[email protected]>
To   <[email protected]>
Subject   st: FW: random number sequence within a loop
Date   Mon, 5 Mar 2007 17:47:50 +0000

Dear Stata users,
 
I'm trying to sample controls for a case-controls study. For each case I
would like to sample one control. I use a loop for this, and "set seed"
to a random number. If I wouldn't do this then STATA will produce the
same random number sequence for each case within the loop, and the
control selection wouldn't be random. Therefore I have used:

.local seed = ceil(10000*uniform())
.set seed `seed'
 
to define the seed within the loop (see also below).This should ensure
that everytime stata goes through the loop, a different random number
sequence is generated (i hope this is correct). However, the problem is
that every time I run stata it will give me a different control for each
case. So, I can't reproduce my outcome. Is there any solution for this?
Thanks.
See my loop below:
 
 
 
forvalues i = 1/`case_num'{
     local seed=ceil(10000*uniform())
     set seed `seed'
     gen random=uniform() 
     sort random
     gen list = _n
     replace control`i'=1 if list==`i'
} 
 
Thanks 
 
Raoul

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index