Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Random date generation


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Random date generation
Date   Wed, 16 Jan 2013 17:12:43 +0000

Often the very mention of dates implies mess and complication, but

1. The format you give is just your choice and gets in the way of easy
calculation.

2. Assuming that you want a sample from a uniform distribution on
these dates, that looks like integers from the corresponding Stata
date interval

. di date("19801201", "YMD")
7640

. di date("20061231", "YMD")
17166

. di 17166 - 7640
9526

. set obs 1000
obs was 0, now 1000

. gen mysample = 7640 + floor(9527 * runiform())

A rough graphical test:

. quantile mysample

. format mysample %td

You can use -ceil()- if you like in a similar but not identical
recipe. The difference is only a little bit. (All puns should be
considered intentional.)

You will want a different format. See -help dates and times-.

Nick

On Wed, Jan 16, 2013 at 4:46 PM, sasso, simone <[email protected]> wrote:

> I am currently working on a large dataset  and I need to generate a random
> date (in the format “yyyymmdd”) for each observation.  These random dates
> must be between the 1 December 1980 (“19801201”) and 31 December 2006
> (“20061231”).

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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index