Statalist


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

RE: st: random date generator


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: random date generator
Date   Thu, 3 Sep 2009 16:08:46 +0100

Handling leap years in Stata is also documented in 

http://www.stata.com/support/faqs/data/leapyear.html

It isn't really a frequently answered question, more a fairly awkward
question. 

Nick 
[email protected] 

Jeph Herrin

I'm not sure that what you doing is correct, but
here is how to get the random date you request:

  gen daysinyear=doy(mdy(12,31,year))
  gen rndday=int(daysinyear*runiform())
  gen rnddate=mdy(1,1,year)+rndday

the first line is to capture the days of year in case
it is a leap year.

[email protected] wrote:
 
> I work with Stata 10 (upgrate for Stata 11 is ordered) and I would 
> appreciate your advices for a "random date generator".
> 
> I have panel Data were survivaltime is a dependent variable.
> 
> Excerpt from my datastructure:
> ID      Year    survivaltime    status          "born"          date
for 
> status 1                date f. st.2            date f. st.3
deliverydate
> 1       2000    1098            0               03Jan2000
10jan2003  
> .                       .               18jun2009
> 1       2001    1098            0               03Jan2000
10jan2003  
> .                       .               18jun2009 
> 1       2002    1098            0               03Jan2000
10jan2003  
> .                       .               18jun2009
> 1       2003    1098            1               03Jan2000
10jan2003  
> .                       .               18jun2009
> 
> 2       2000    3453            0               04Jan2000       .
.  
>         .               18jun2009
> 2       2001    3453            0               04Jan2000       .
.  
>         .               18jun2009
> 2       2002    3453            0               04Jan2000       .
.  
>         .               18jun2009
> 
> 3       2000    3453            0               04Jan2000       .
.  
>         .               18jun2009
> 3       2001    3453            0               04Jan2000       .
.  
>         .               18jun2009
> 3       2002    3453            3               04Jan2000       .
.  
>         .               18jun2009
> 
> 
> The variable status has the numbers 0,1,2,3 and they mean 0=pending,
and 
> 1,2,3 = "death". If the person "dies" because of reason 1 I have 
> information in the colum "date for status 1" and date f.st.2 and date
f. 
> st.3 are missing. The same system is for status 2 and 3. If the person

> does not die, the status in the last observed year is 0 and i have no 
> dates.
> 
> The survivaltime I computed with: 
> replace survivaltime=date for status 1-"born" if (date for status 1!=.
& 
> status!=.) etc
> and for the pendings:
> replace survivaltime=deliverydate-"born" if (date for status 1==. &
date 
> f. st.2==. & date f. st.3==.)
> 
> As you can see I computed the survivaltime not depending on the status

> (because I did not know how to work around the change from 0 to a
status 
> of 1-3) but I assumed that if I have Status 1 there has to be a date
in 
> "date for status 1".
> 
> And here is my problem, because, as you can see at ID 3, sometimes I
have 
> a status (in this case 3) but I do not have date information in "date
f. 
> st.3". Because of this my survivaltime is computed wrong.
> 
> Because I know the year when the person "died" I would like to
randomly 
> generate a "death date" between 01.01. and 31.12. in the death year
for 
> the correct colum? (So that if the person dies because auf reason 3 I
get 
> a date in colum "date f st.3" and not only for the year of death (in
case 
> of ID3 2002) but also the same date in 200 and 2001, as seen in the 
> excerpt.
> 
> Could somebody help my how to do this?
> 
> Also I would like to know how to change the pending state in the years

> before the death. So that I do not have 
> 
> ID      Year    survivaltime    status           but have       ID
Year 
> survivaltime    status
> 1       2000    1098            0                               1 2000

> 1098            1 
> 1       2001    1098            0                               1 2001

> 1098            1
> 1       2002    1098            0                               1 2002

> 1098            1
> 1       2003    1098            1                               1 2003

> 1098            1

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



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index