Statalist The Stata Listserver


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

Re: st: Generating Random Number


From   Joseph Coveney <[email protected]>
To   Statalist <[email protected]>
Subject   Re: st: Generating Random Number
Date   Wed, 24 Jan 2007 08:42:51 +0900

Raphael Fraser wrote:

I am only interested in uniqueness.

I have 100 patients with ids 1-200. There are two observations per id.
I would like to generate another id which I call bsid which ranges
between 301-500.

--------------------------------------------------------------------------------

Try something like that below.  The first few lines just create the dataset
that you describe.  The rest create the other id that meets your
specifications, and merges it with the dataset so that you have a so-called
cross-walk table that you can use to decipher bsid in terms of the original
patient ID.

With only 200 patients, you probably won't traverse thel loop more than
once; with tens of thousands or more, you might traverse the loop a couple
of times.  I think that the most that I've ever done with something similar
is three times through, and that was to select a random sample of case
report form fields for audit of a 60-page set of case report forms for a
4000-patient study.  (And even there, as I recall, I forgot to type "double"
after -generate-.)

Joseph Coveney

clear
set more off
tempfile tmpfil0
set obs 200
*
*  Creating patient dataset
*
generate int id = _n
forvalues i = 1/2 {
   generate str observation`i' = "An observation"
}
*
* Begin here
*
generate int row = _n
sort row
save `tmpfil0'
local count = _N
*
clear
set obs `count'
set seed `=date("2007-01-24", "ymd")'
generate int bsid = `count' + 100 + _n
local quit = 1
while (`quit') {
   generate double randu`quit' = uniform()
   sort randu`quit', stable
   capture assert randu`quit' > randu`quit'[_n-1] in 2/l
   if _rc `++quit'
   else continue, break
}
generate int row = _n
sort row
merge row using `tmpfil0'
assert _merge == 3
drop _merge row
erase `tmpfil0'
exit

*
*   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