Hi,

I would like to ask again the same question for sorting the observations by descending orders and only keep the first observation.

For instance, the data looks like this:

Code:
State County Programtype Begin Date 1 2 6 2007/01/01 1 2 6 2008/01/01 1 2 5 2009/01/01 2 3 4 2006/01/01 2 3 3 2008/01/01 2 3 3 2011/01/01
I would like to first sort the data and keep only 1 observation by state/county/programtype with the most recent begin start date.

When I tried the code, this did not work with my example, saying that the variables are already defined.

Why not sort the observations in ascending order and only keep the last observation for each state/county?
Code:
. * Example generated by -dataex-. To install: ssc install dataex
. clear

. input byte(state county programtype) float begin

state county progra~e begin
1. 1 2 6 17167
2. 1 2 6 17532
3. 1 2 5 17898
4. 2 3 4 16802
5. 2 3 3 17532
6. 2 3 3 18628
7. end

. format %td begin

. by state county (begin), sort: generate tokeep = _n==_N

. list, sepby(state county)

+------------------------------------------------+
| state county progra~e begin tokeep |
|------------------------------------------------|
1. | 1 2 6 01jan2007 0 |
2. | 1 2 6 01jan2008 0 |
3. | 1 2 5 01jan2009 1 |
|------------------------------------------------|
4. | 2 3 4 01jan2006 0 |
5. | 2 3 3 01jan2008 0 |
6. | 2 3 3 01jan2011 1 |
+------------------------------------------------+
is there an alternative way to solve this?

Thank you very much for your help!