Hi to everyone!

I am writing an article with use of event study. Unfortunately I am new to Stata and experience a lot of difficulties with this program.
I ve used this link https://dss.princeton.edu/online_hel...udy.html#clean as benchmark.

So what I have. Let's say that I have trial for 20 companies at first. So everything goes right with estimation if returns and event windows.
The problem (or I would better say my lack of knowledge) arises in testing market model.
As far as I am concerned I need market returns for such procedure. I ve downloaded S&P500. But I am kind of lost about how should I match it to my data. I mean every firm have their own date set.
Of course I can do it by hands and fit copy.past by firm individually but it seems really inefficient when my sample will go much more larger than just 20 firms.
Can anyone give me an advice?

I am really sorry if this question is super stupid, but I am so lost at my Stata skills that I do not know what else should I do.

I am attaching my current code

*format
format date %td
format edate %td

*calculating event windows
sort company_id date
by company_id: gen datenum=_n
by company_id: gen target=datenum if date==edate
egen td=min(target), by(company_id)
drop target
gen dif=datenum-td


by company_id: gen event_window=1 if dif>=-5 & dif<=5
egen count_event_obs=count(event_window), by(company_id)
by company_id: gen estimation_window=1 if dif<-30 & dif>=-60
egen count_est_obs=count(estimation_window), by(company_id)
replace event_window=0 if event_window==.
replace estimation_window=0 if estimation_window==.


tab company_id if count_event_obs<5
tab company_id if count_est_obs<30

drop if count_event_obs < 2
drop if count_est_obs < 30


so next step should probably be normal returns - so I wonder how to match market return with firms id.