I am not sure whether the following code works best for the purpose. Based on my data, it takes around two days to run from 2010-2019. I also intend to run data since 1990s, so it may take several days. I think it is a bit long.
If there is a better way to save time, please let me know and thank you so much.

gen relative_FB_all = .
gen relative_Forecast_horizon_all = .
gen relative_dayElap_all = .
gen relative_forecast_frequency_all = .
gen relative_firm_specific_all = .
gen relative_generic_all = .
gen relative_cos_followed_all = .
gen relative_indus_followed_all = .
gen relative_analyst_broke_all = .

destring gvkey, replace

levelsof fiscal_year, local(years)
foreach year of local years {

levelsof gvkey if fiscal_year == `year', local(companies)
foreach company of local companies {

levelsof analys if fiscal_year ==`year' & gvkey==`company', local(analysts)
foreach analyst of local analysts {

su forecast_bias ///
if analys!=`analyst' & fiscal_year==`year' & gvkey==`company'
replace relative_FB_all=r(mean) ///
if analys!=`analyst' & fiscal_year==`year' & gvkey==`company'

di `year'
di `company'
di `analyst'
}
}
}