Dear all,
I meet an issue when generting days elapsed from IBES data. Based on the literature, the definition is as follows:

DaysElapsedijt is a measure of the days elapsed since the last forecast by any analyst following firm j in year t, calculated as the days between analyst i’s forecast of firm j’s earnings in year t and the most recent preceding forecast of firm j’s earnings by any analyst, minus the minimum number of days between two adjacent forecasts of firm j’s earnings by any two analysts in year t, with this difference scaled by the range of days between two adjacent forecasts of firm j’s earnings in year t.

I write the code as follows, whereas I noticed there exists negative days in my generated variable:

gen fiscal_day=day(fpedats)
gen forecasted_day=day(anndats)
gen actual_announce_day=day(anndats_act)
sort gvkey fiscal_year forecasted_year forecasted_month forecasted_day
by gvkey fiscal_year: gen number_obs=_n
by gvkey fiscal_year: egen max_number_obs=max(number_obs)
gen last_day=forecasted_day-actual_announce_day
by gvkey fiscal_year: egen dayElap=max(last_day)


I output a sample as follows, and very much looking forward to your response. Thank you.

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str6 gvkey float(fiscal_year forecasted_year forecasted_month) long(fpedats anndats anndats_act)
"016134" 2011 2011  9 18992 18895 19052
"016134" 2011 2011  7 18992 18809 19024
"016134" 2011 2011 11 18992 18938 19031
"016134" 2011 2011  6 18992 18799 19060
"016134" 2011 2011  5 18992 18778 19061
"016134" 2011 2011  9 18992 18890 19032
"016134" 2011 2011  9 18992 18877 19075
"016134" 2011 2011  7 18992 18813 19046
"016134" 2011 2011  5 18992 18764 19025
"016134" 2011 2011  6 18992 18791 19032
"016134" 2011 2011  5 18992 18756 19066
"016134" 2011 2011  9 18992 18876 19058
"016134" 2011 2011  7 18992 18820 19024
"016134" 2011 2011  3 18992 18708 19037
"016134" 2011 2011  7 18992 18813 19046
"016134" 2011 2012  3 18992 19059 19066
"016134" 2011 2011  9 18992 18890 19032
"016134" 2011 2012  1 18992 19018 19072
"016134" 2011 2011  6 18992 18801 19067
"016134" 2011 2011 10 18992 18903 19024
"016134" 2011 2011  6 18992 18806 19124
"016134" 2011 2012  1 18992 19016 19072
"016134" 2011 2012  3 18992 19071 19079
"016134" 2011 2011  6 18992 18794 19067
"016134" 2011 2011  6 18992 18800 19058
"016134" 2011 2011  9 18992 18877 19038
"016134" 2011 2011  8 18992 18864 19052
"016134" 2011 2012  1 18992 19001 19075
"016134" 2011 2011  5 18992 18760 19037
"016134" 2011 2011 10 18992 18904 19024
"016134" 2011 2011  3 18992 18694     .
"016134" 2011 2011  5 18992 18757 19029
"016134" 2011 2012  1 18992 19001 19045
"016134" 2011 2011  5 18992 18756 19033
"016134" 2011 2012  1 18992 19022 19072
"016134" 2011 2011  6 18992 18801 19064
"016134" 2011 2011  5 18992 18750 19066
"016134" 2011 2011  7 18992 18816 19074
"016134" 2011 2012  2 18992 19029 19080
"016134" 2011 2012  1 18992 18997 19059
"016134" 2011 2012  1 18992 19015 19080
"016134" 2011 2011  7 18992 18809 19024
"016134" 2011 2011  5 18992 18756 19033
"016134" 2011 2011  6 18992 18806 19067
"016134" 2011 2011 12 18992 18989 19038
"016134" 2011 2012  1 18992 19015 19080
"016134" 2011 2011 11 18992 18941 19050
"016134" 2011 2011  8 18992 18847 19060
"016134" 2011 2011  8 18992 18862 19058
"016134" 2011 2011  6 18992 18807 19067
"016134" 2011 2011 12 18992 18989 19038
"016134" 2011 2011  9 18992 18890 19032
"016134" 2011 2011  9 18992 18878 19058
"016134" 2011 2011  9 18992 18878 19038
"016134" 2011 2011  5 18992 18766 19032
"016134" 2011 2011  6 18992 18807 19059
"016134" 2011 2011  3 18992 18701 19037
"016134" 2011 2011  5 18992 18773 19032
"016134" 2011 2012  1 18992 19015 19080
"016134" 2011 2011 12 18992 18982 19075
"016134" 2011 2011  9 18992 18876 19058
"016134" 2011 2011  8 18992 18849 19066
"016134" 2011 2012  1 18992 19018 19072
"016134" 2011 2011 12 18992 18982 19075
"016134" 2011 2012  2 18992 19029 19058
"016134" 2011 2011  7 18992 18816 19024
"016134" 2011 2011 12 18992 18982 19081
"016134" 2011 2011 12 18992 18982 19082
"016134" 2011 2011  9 18992 18876 19038
"016134" 2011 2011 12 18992 18980 19032
"016134" 2011 2011 12 18992 18982 19075
"016134" 2011 2011  6 18992 18808 19067
"016134" 2011 2011  5 18992 18756 19036
"016134" 2011 2012  1 18992 19015 19080
"016134" 2011 2011 11 18992 18945 19031
"016134" 2011 2012  1 18992 18996 19059
"016134" 2011 2011 12 18992 18989 19038
"016134" 2011 2011  7 18992 18813 19046
"016134" 2011 2011 11 18992 18953 19081
"016134" 2011 2011  6 18992 18781 19213
"016134" 2011 2011  9 18992 18878 19064
"016134" 2011 2011  5 18992 18760 19033
"016134" 2011 2011  5 18992 18760 19033
"016134" 2011 2011  6 18992 18801 19058
"016134" 2011 2011  3 18992 18700 19037
"016134" 2011 2011  7 18992 18822 19066
"016134" 2011 2011  5 18992 18756 19033
"016134" 2011 2011  8 18992 18847 19050
"016134" 2011 2011  5 18992 18756 19033
"016134" 2011 2011  9 18992 18900 19025
"016134" 2011 2012  1 18992 18995 19045
"016134" 2011 2011  9 18992 18876 19038
"016134" 2011 2011 10 18992 18905 19025
"016134" 2011 2011  8 18992 18870 19058
"016134" 2011 2011  7 18992 18813 19024
"016134" 2011 2011  7 18992 18809 19024
"016134" 2011 2011  7 18992 18813 19067
"016134" 2011 2011  9 18992 18879 19052
"016134" 2011 2012  1 18992 18996 19031
"016134" 2011 2011  6 18992 18779 19061
end
format %d fpedats
format %d anndats
format %d anndats_act
label var gvkey "Global Company Key" 
label var fpedats "Forecast Period End Date, SAS Format" 
label var anndats "Announce Date, SAS Format" 
label var anndats_act "Announce date of the Actual, from the Actuals File"