Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

st: keep only one observation


From   Lars Folkestad <lfolkestad@health.sdu.dk>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   st: keep only one observation
Date   Tue, 5 Jun 2012 22:09:22 +0200

Dear list

I have a dataset of measured continuous data. The observations are from different participants. In my database i have 10.000+ observations.  I have an unique identifier pr participant. Some participants have been measured more than once in the observational period (on different dates). And for most participants i have more than one measurement pr date (for simplicity lets say they did not live up to strict quality measures and thus are rendered useless).

I want to delete all other than the last observation pr participants on the different dates.

I've tried to generate a date sequence variable (seq) by

bysort id (date_var) : gen seq = _n

But this does not give me what i want. I get a sequence number pr id but it does not seem to take into account the date_var.

Fx

What i get
Id date seq
1 01.01.01 1
1 01.01.01 2
1 01.02.02 3
1 01.04.05 4


What i want
Id date seq
1 01.01.01 1
1 01.01.01 2
1 01.02.02 1
1 01.04.05 1


So a two part question:
How do i get stata to give a sequence variable that takes date and id into account?
How do i then drop all but the last (the larges seq nr) from the list taking id into account?

Hope this is somewhat clear.

lars

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index