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

# Re: st: Random date generation

 From Ryan Kessler To statalist@hsphsun2.harvard.edu Subject Re: st: Random date generation Date Wed, 16 Jan 2013 12:17:53 -0500

```Along the same lines:

clear
set obs 1000
generate random_date =
floor((mdy(12,31,2006)-mdy(12,1,1980)+1)*runiform() + mdy(12,1,1980))
format random_date %tdCCYYNNDD

Ryan

On Wed, Jan 16, 2013 at 12:12 PM, Nick Cox <njcoxstata@gmail.com> wrote:
> 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 <sasso@merit.unu.edu> 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/

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