Hey!

I have a panel data. cusip is the company ID. fpedates is the quarter end date. anndates is the announce date for analysts. analys is the id of analysts

For every fpedates of each cusip, I want to keep only one analys (the one with the most recent anndates).

Also, anndates needs to be ahead of fpedates but not more than 50 days before.

In addition, I need there are at least two analys for each cusip in every quarter (drop the cusip quarters with less than two analys).

Anyone can help? Great thanks!

Here are the data example:


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str8 cusip double analys long(fpedats anndats)
"25468710"  54440 21639 21586
"25468710"   9584 21639 21586
"25468710"  71755 21639 21586
"25468710"  46442 21639 21585
"25468710"  54310 21639 21586
"25468710" 595335 21639 21584
"25468710" 582439 21639 21586
"25468710" 500876 21639 21586
"25468710" 554250 21639 21586
"25468710"   1717 21639 21586
"25468710" 593008 21639 21585
"25468710" 581246 21639 21586
"25468710"  82890 21639 21586
"25468710" 121968 21639 21587
"25468710" 564583 21639 21586
"25468710" 507503 21639 21587
"25468710" 595335 21639 21587
"25468710" 111523 21639 21586
"25468710"  19083 21639 21590
"25468710"  71755 21639 21593
"25468710" 106603 21639 21599
"25468710"  71755 21639 21614
"25468710" 554250 21639 21628
"25468710"  46442 21639 21630
"25468710" 582439 21639 21635
"25468710" 111523 21639 21640
"25468710" 137702 21639 21641
"25468710" 500876 21639 21643
"25468710"  46442 21639 21643
"25468710" 535432 21639 21647
"25468710" 621768 21639 21647
"25468710" 564583 21639 21649
"25468710"   9584 21639 21651
"25468710"   1717 21639 21651
"25468710" 500876 21639 21651
"25468710"  71755 21639 21654
"25468710" 119704 21639 21662
"25468710" 593008 21639 21664
"25468710" 581246 21639 21668
"25468710" 106603 21639 21668
"25468710" 593008 21639 21669
"25468710" 621768 21639 21668
"25468710" 507503 21639 21670
"25468710" 137702 21639 21675
"25468710" 564583 21639 21675
"25468710" 621768 21639 21675
"25468710"  46442 21639 21653
"25468710"   1717 21639 21677
"25468710"  54440 21730 21678
"25468710"  82890 21730 21678
"25468710"  46442 21730 21677
"25468710"  54310 21730 21678
"25468710" 137702 21730 21678
"25468710"   1717 21730 21678
"25468710" 595335 21730 21678
"25468710" 554250 21730 21678
"25468710" 500876 21730 21678
"25468710"   9584 21730 21678
"25468710" 581246 21730 21678
"25468710" 593008 21730 21678
"25468710" 507503 21730 21679
"25468710" 106603 21730 21678
"25468710" 564583 21730 21681
"25468710" 111523 21730 21677
"25468710" 119704 21730 21683
"25468710" 621768 21730 21684
"25468710"  71755 21730 21689
"25468710"  54440 21730 21702
"25468710" 535432 21730 21713
"25468710"   1717 21730 21714
"25468710"  71755 21730 21717
"25468710" 554250 21730 21727
"25468710"  54310 21730 21739
"25468710"  46442 21730 21740
"25468710" 137702 21730 21755
"25468710" 564583 21730 21761
"25468710" 500876 21730 21763
"25468710" 554250 21822 21767
"25468710"  54440 21822 21768
"25468710"   9584 21822 21768
"25468710"  82890 21822 21768
"25468710"  46442 21822 21767
"25468710" 119704 21822 21768
"25468710"  54310 21822 21768
"25468710" 564583 21822 21768
"25468710" 581246 21822 21768
"25468710" 137702 21822 21768
"25468710" 500876 21822 21768
"25468710"  54440 21822 21769
"25468710" 507503 21822 21770
"25468710" 106603 21822 21773
"25468710" 111523 21822 21767
"25468710"  71755 21822 21780
"25468710" 621768 21822 21780
"25468710"  71755 21822 21781
"25468710"   1717 21822 21791
"25468710"  46442 21822 21793
"25468710" 593008 21822 21803
"25468710" 119704 21822 21805
end
format %d fpedats
format %d anndats