Dear all,
I am posting a few observation of my data below.
Ticker: firm unique id
Analys: financial analyst id
Value: earnings forecast issued by analyst
Anndats: the date forecast is issued
Fyear: forecast year
You shall see that some analysts issue multiple forecasts for a year, e.g. analys 79092 updates his/her forecasts 7 times in year 2014.
I want to generate a count of how many distinct analysts issuing forecast each firm per year, in above example, analys 79092 should be counted once, not 7 times, that is , the variable I want to generate, call it count, count = 6 for the firm in the posted data.
Hence, I try to use the following code to keep the last forecast per analyst firm year, but it does not delete 6 out of the 7 observations for analys 79092
Could you point out my error ?
thank you ,
Rochelle
I am posting a few observation of my data below.
Ticker: firm unique id
Analys: financial analyst id
Value: earnings forecast issued by analyst
Anndats: the date forecast is issued
Fyear: forecast year
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str8 cusip long analys double value long anndats int fyear "87482X10" 50789 1.14 20141107 2014 "87482X10" 71182 .86 20140317 2014 "87482X10" 71182 1.02 20140507 2014 "87482X10" 71182 1.24 20140807 2014 "87482X10" 71182 1.21 20141105 2014 "87482X10" 72481 1.2 20141203 2014 "87482X10" 79092 .8 20140310 2014 "87482X10" 79092 .51 20140407 2014 "87482X10" 79092 .58 20140508 2014 "87482X10" 79092 .57 20140709 2014 "87482X10" 79092 1.23 20140808 2014 "87482X10" 79092 .65 20140815 2014 "87482X10" 79092 1.2 20141106 2014 "87482X10" 80474 .83 20140310 2014 "87482X10" 80474 1.05 20140317 2014 "87482X10" 80474 .56 20140407 2014 "87482X10" 80474 .58 20140507 2014 "87482X10" 80474 .59 20140603 2014 "87482X10" 80474 1.09 20140807 2014 "87482X10" 80474 1.22 20141105 2014 "87482X10" 119962 .73 20140309 2014 "87482X10" 119962 .5 20140409 2014 "87482X10" 119962 .58 20140507 2014 "87482X10" 119962 1.17 20140807 2014 "87482X10" 119962 1.19 20141105 2014 end
I want to generate a count of how many distinct analysts issuing forecast each firm per year, in above example, analys 79092 should be counted once, not 7 times, that is , the variable I want to generate, call it count, count = 6 for the firm in the posted data.
Hence, I try to use the following code to keep the last forecast per analyst firm year, but it does not delete 6 out of the 7 observations for analys 79092
Code:
sort ticker fyear analys anndats by ticker fyear analys anndats, sort: keep if _n == _N //(31,606 observations deleted) * count # of analysts by ticker fyear , sort: gen analyno=_N // I got analyno=28 which counts same analyst multiple times.
thank you ,
Rochelle
Comment