program randgen, sortpreserve
syntax anything, list(string)
tempvar rnd
generate `rnd'=uniform()
sort `rnd'
egen `anything'=fill(`list' `list')
end
Use it like this:
randgen testvar, list(10000 10010 10020)
(Note that this will guarantee you that you will have equal number of
observations (or 1 less) in each Kreis). This is different from gen
Kreis=F(uniform()), because in theory uniform() may evaluate to a
single value for all your observations, but with a very very very
small probability. The program above however, randomizes order of
observations, then assigns the pattern, then restores the order.
Hope this helps.
Regards,
Sergiy Radyakin
On 8/15/08, Martin Weiss <[email protected]> wrote:
> The problem is not entirely clear to me, but sounds like a case for nested
> -cond- statements as in
>
> ********
> clear*
> set obs 10000
> g x=cond(runiform()<0.3, 10000, cond(runiform()<0.6, 10010,
> cond(runiform()<0.9,10020, 10030)))
> *******
>
> where x is drawn from (10000,10010,10020,10030) and you can specify the
> probabilities for the runiform() function as you please...
>
> HTH
> Martin
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Jochen Sp�th
> Sent: Friday, August 15, 2008 5:21 PM
> To: [email protected]
> Subject: st: generating random numbers from a specified list
>
> Dear statalisters,
>
> trying to build up a new dataset which is (almost) entirely random I'm
> confronted with the following problem: How can I tell Stata to generate a
> variable by randomly picking numbers from a (previously) specified list?
> Here's a minimal example of what I want to do:
>
>
> Data right looks like follows:
>
> firm year az_ges az_ges_vz
> 1000129 1975 15 11
> 1000129 1976 14 11
> 1000129 1977 8 6
> 1000129 1978 26 20
>
> 1000530 1993 12 9
> 1000530 1994 29 22
> 1000530 1995 26 20
> 1000530 1996 14 11
> 1000530 1997 18 14
>
> and so on, where firm is the cross section identifier and year the time
> variable. Note that data ist organised in long format.
> Now, I'd like to add a variable which contains for each firm a number picked
> at random from a specified list, f.i. list=(10000,10005,10010, and so on).
> This "random" number should be the same for firms across years. The list
> exists already, albeit in a separate data file that can't be merged with the
> data set at hand because it lacks the identifiers betnr and year.
>
> Any suggestions?
>
> Thanks,
>
> Jochen
>
> ----------------------------------------------------------------------------
> ---------------
>
> Jochen Sp�th
> Dipl.-Volkswirt
> Institut f�r Angewandte Wirtschaftsforschung (IAW) T�bingen
> Ob dem Himmelreich 1
> 72074 T�bingen
> Tel.: +49-(0)7071-9896-14
> Fax: +49-(0)7071-9896-99
> EMail: [email protected]
> IAW-Homepage: www.iaw.edu
>
> ----------------------------------------------------------------------------
> ---------------
>
>
>
> *
> * 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/
>
>
> *
> * 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/
>
*
* 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/