st: AW: How to generate a variable equalling an earliest date of action b y individual?

 From "Martin Weiss" To Subject st: AW: How to generate a variable equalling an earliest date of action b y individual? Date Sat, 19 Sep 2009 18:26:13 +0200

<>

Also note http://www.stata.com/support/faqs/lang/sort.html

HTH
Martin

-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Ekaterina
Hertog
Gesendet: Samstag, 19. September 2009 18:23
An: statalist@hsphsun2.harvard.edu
Betreff: st: How to generate a variable equalling an earliest date of action
b y individual?

Dear all,
I have a dataset of individuals each of whom approaches a number of others
and after approaching each person is either accepted or rejected. I would
like to do an event history analysis of how the duration of time it takes
for an individual to secure an acceptance depends on his or hers individual
characteristics.
The variables I am interested in have the following structure (missing
dateacceppted means a rejection):

id	dateapproached	dateaccepted
1	20.04.2007			.
1	20.04.2007			.
1	18.05.2007		20.05.2007
1	18.05.2007			.
2	19.01.2006			.
2	20.03.2007		24.03.2007
2	20.03.2007			.
3	11.09.2007		18.09.2007
3	11.09.2007		15.09.2007
3	01.10.2007			.
3	07.11.2007			.
4	06.06.2007			.
4	08.07.2007			.

Out of this dataset I want to keep for each individual the date he or she
made his first approach and the date he or she was first accepted.
To do so I tried to create a new variable date1accept which for each
individual has the same value => the date he or she was first accepted.
After that I dropped all the observations, but the 1st one for each
individual.
To do so I used the following code:
sort id dateaccept
foreach X of new date1accept {
by id : gen `X' = dateaccept[1]
}

sort id
by id: gen y=_n
keep if y==1

I then ran two tests and Stata confirmed that thes assertions below were
true:
/*To check that all the values of date1accept are the same*/
bysort id (date1accept) : assert date1accept[1] == date1accept[_N]
/*To check that dat1accept = the lowest value of dateaccept*/
bysort id (dateaccept) : assert dat1accept[1] == dateaccept[1]

yet something seems to be wrong with my commands, each time I re-run the
code it comes up with slightly different set of date1accept values. I cannot
understand where can my code be wrong so my tests do not pick it up.
Could anyone suggest what might be wrong or offer an alternative solution
for my problem?
I will be most grateful for any advice,
Warm regards,
Ekaterina

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