Quantcast
Channel: Statalist
Viewing all articles
Browse latest Browse all 72764

Numbering Cases Based on The Sorting Of Two Variables

$
0
0
Hi all,

I come to you with a problem that I suspect will be like falling off a log for some of you. I have been noodling around for a few hours and now turn to the collective brilliance of the list hoping to put me out of my misery.

Assume I have two variables: id and date. Date is a non-string (i.e., numeric) variable. id is not unique but for each pair of ids I have two unique dates. I would like to order the cases from 1 to 3 within each id depending on the date order.

So for example this is what I would like to get:
id date duporder
1 1/7/2016 2
1 2/22/2012 1
2 2/22/2012 1
2 1/14/2016 2
3 3/1/2012 1
I have tried the following with no success:

gsort -date
bysort id: gen duporder=_n

or

gsort date
bysort id: gen duporder2=_n

But have found it is not consistently providing the duporder in the correct order.

Here is output from the editor
id date duporder duporder2
1 1/7/2016 2 2
1 2/22/2012 1 1
2 2/22/2012 1 2
2 1/14/2016 2 1
3 2/22/2012 2 2
3 1/6/2016 1 1
4 1/14/2016 1 2
4 3/1/2012 2 1

Any immediate ideas as to what I am doing wrong?

Would you approach it differently?

Thanks, in advance, for any help you can offer.

Ben Hoen
Berkeley Lab



Viewing all articles
Browse latest Browse all 72764

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>