Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# st: Pseudorandom Number from Supplied Table of Probabilities (Equivalent of SAS Rand("Table") Function)

 From Mustafa Hirji To "statalist@hsphsun2.harvard.edu" Subject st: Pseudorandom Number from Supplied Table of Probabilities (Equivalent of SAS Rand("Table") Function) Date Mon, 6 Aug 2012 12:51:19 +0000

```Greetings,

I would like to select one number from a a pre-defined set, where the probability of selecting each number is known. The analogous case if my set had only two numbers would be a Bernoulli event which would be the equivalent of using

rbinomial(1, p)

where p is the probability of one of the events (and, obviously, (1-p) being the probability of the other).

I am writing code where my set will be larger than just two items, however.

SAS has a function

rand("table", p1, p2, …, pn)

that does this as is explained on this page (click "Tabled Distribution" from the contents list):

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a001466748.htm

1.  Is there anything similar in Stata. I cannot find anything.

2.  If not, how do you recommend I implement this? I have to thoughts:

(a)	manufacture a dataset with 100,000 observations (or any large number), populate a single variable from my set in proportions corresponding to the probability, shuffle it, and then randomly sample one observation. There's a slight loss of precision here, and it's inelegant in that I'd need to save my current dataset to create this one, and I have a few hundred cases where I need to run this.

(b)	I could loop through each element in my set, and execute the Bernoulli probability

rbinomial(1, p)

for each element until I get a non-zero result. p would be the probability corresponding to that element in the set divided by the cumulative probability of all remaining elements, inclusive, so as not to bias my result based on ordering. If nothing returned non-zero, I'd select the final element of my self. I'm not entirely sure that this approach is statistically correct, however.

Any advice you can provide would be much appreciated!

Mustafa Hirji

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```